Search code examples
javascriptapimarkermanager

Adding a single point with google API javascript v3 MarkerManager


Hey all, I'm having problems on adding a single marker to a google map v3 using the marker manager. I understand that the manager is better suited for adding arrays, but I wish to understand how to add a single marker as part of the learning process. Can anyone point me in the correct direction? Here is the code I am currently working with to add a single marker:

<DOCTYPE! html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(29.448903234142804, -98.51985454559326);
var myOptions = {
  zoom: 10,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions); 

mgr = new MarkerManager(map);

var myLatlng = new google.maps.LatLng(29.448903234142804, -98.51985454559326);

var marker = new google.maps.Marker({
  position: myLatlng, 
  map: map, 
  title:"Hello World!"});
mgr.addMarker(marker,9,12);
mgr.refresh();}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>

Solution

  • If you were getting an error "google is not defined" place the link to markermanager.js e.g. <script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/src/markermanager.js"></script> within or after the BODY section of your HTML file.

    ========

    Update: And now it works as you wish - tested!!

    You have to do two things: 1st: load the MarkerManager source in the HEAD by:

    <script type="text/javascript">
    document.write('<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/src/markermanager.js"><'+'/script>');
    </script>
    

    2nd: Tell manager to add your marker after the map is loaded:

    google.maps.event.addListener(mgr, 'loaded', function(){
    mgr.addMarker(marker,9,12);
    mgr.refresh();});
    }
    

    Good luck

    K