I have implemented code that adds a marker at place, with the function being called via update(). However, one marker is only ever added, and the location of that marker is seemingly "random" for anywhere on the map focussed on Cambridge, MA. I am assuming there is an error in my code, or is it just a symptom of not yet having implemented removeMarker? Thanks in advance. My addMarker code is below:

    function addMarker(place) {

        markers = place;

        var image = 'http://maps.google.com/mapfiles/kml/pal2/icon31.png';        

        function initialize() {
          var myLatlng = new google.maps.LatLng(markers.latitude,markers.longitude);
          var mapOptions = {
            center: myLatlng,
            zoom: 13
            var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

          var marker = new MarkerWithLabel({
            position: myLatlng,
            map: map,
            icon: image,
            labelClass: "labels",
            labelAnchor: new google.maps.Point(15,0),
            labelContent: markers.place_name,

          google.maps.event.addListener(marker, 'click', function() {

          var infowindow = new google.maps.InfoWindow({

            //infowindow content
            content: articles
            //infowindow content


          google.maps.event.addDomListener(window, 'load', initialize);  

place.latitude (and longitude) is a string (at least in my case). I use parseFloat to convert it to a number.

myLatLng = new google.maps.LatLng(parseFloat(place.latitude), parseFloat(place.longitude));

The for the addListener you need to point to a function that loads the JSON data - I added a function called loadinfo()

 google.maps.event.addListener(marker, "click", function() { loadinfo(marker, place.postal_code  ); });
  • Thanks @gitterb, ensuring the lat and lng are numbers has helped remove errors. However, it transpires that most of the issues were due to my vastly overcomplicated code,as below! – AlastairHoward Feb 25 '15 at 18:59

After working on this problem further, I realised my problem was caused by initialising the map within my addMarker function. This created two issues: the first is that the source code already initialises the map, and so it was replacing this unnecessarily. The second issue was that each time addMarker is called in a loop such that multiple markers are added, the map is reinitialised and so the only marker visible is the last to be added.

To get this working, make the code much simpler. Focus only on actually adding the marker by implementing only the new MarkerWithLabel function and using the code and variables supplied earlier in the source code to attach it to the map.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .