user2719726
user2719726

Reputation: 11

Multiple Markers on google maps v3 not working in loop

Okay guys and gals, I am having some issues with adding multiple markers. I swear I have searched till my eyes are crossed and I just cannot figure this out.

Here is my code:

function initialize()
{
cnt = 0;

var icon = "bullet-red-icon.png";

var mapProp = {
    center: new google.maps.LatLng(39.8282,-98.5795),
    zoom:5,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

$("marker", mapxml).each(
    function(i)
    {
        var iwcontent = $(this).find("mname").text() + "<br /><br />";
        iwcontent += $(this).find("mstreet").text() + "<br />" + $(this).find("mcity").text() + " " + $(this).find("mstate").text() + " " + $(this).find("mzip").text() + "<br /><br />";
        iwcontent += "UNITS: " + $(this).find("units").text() + "<br />";
        iwcontent += "UNIT COST: " + $(this).find("unitcost").text() + "<br />";
        iwcontent += "TOTAL FUEL COST: " + $(this).find("fuelcost").text() + "<br />";

        var mlat = parseFloat($(this).find("mlat").text());
        var mlon = parseFloat($(this).find("mlon").text());

        addMarker(iwcontent, mlat, mlon, map, i);
    }
);

$("#d_transmap").show();
$(".t_loader").hide();

}

Now I know all the data is there, I have console.logged everything and all seems fine. However no markers show up at all.

Here is the addMarker function:

function addMarker(content, lat, lon, map, i)
{
    var point = new google.maps.LatLng(lat, lon.toFixed(6));
    var marker = new google.maps.Marker({ position: point, icon: "bullet-red-icon.png", map: map });
    var infowindow = new google.maps.InfoWindow({ content: content });
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(content);
            infowindow.open(map, marker);
        }
    })(marker, i));
}

Now, if I go outside the loop and call this:

addMarker(content, 39.8282, -98.5795, map, i)

It will spit out a marker! So my initial thought was something is wrong with my lat long data. However I logged all of those, and even logged the markers themselves and it seems to create them. I just do not see them.

What in the world am I doing wrong?

Upvotes: 1

Views: 1517

Answers (2)

Michael Geary
Michael Geary

Reputation: 28880

Your addMarker() function has a couple of problems.

  1. The call to lon.toFixed(6) shouldn't be there. Just use lon directly. Also I suggest using the name lng instead of lon for consistency with the Maps API terminology.
  2. You don't need the fancy song and dance with the function-returning-a-function for the click event listener. You already have a closure here because addMarker() is a function. You don't need yet another closure. And you don't need the i parameter at all. This isn't preventing your code from working, but it's a lot of extra complication you don't need.
function addMarker(content, lat, lng, map) {
    var point = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({
        position: point,
        icon: "bullet-red-icon.png",
        map: map
    });
    var infowindow = new google.maps.InfoWindow({ content: content });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(content);
        infowindow.open(map, marker);
    });
}

Update: Zoom all the way out in your test page so you can see Antarctica. Found your missing markers? :-)

(Update 2: Well, sometimes I see the markers there and sometimes I don't. But keep reading...)

Now check the latitudes and longitudes in your XML download, for example:

<mlat>-82.753936</mlat>
<mlon>42.675168</mlon>

Upvotes: 1

Shivam
Shivam

Reputation: 2443

use below code for multiple marker variable arr is a array of lat long value

   function initialize(arr) {

  directionsDisplay = new google.maps.DirectionsRenderer();
  geocoder = new google.maps.Geocoder();

  var mapOptions = {     
  };

  var icon = "bullet-red-icon.png";
  map = new google.maps.Map(document.getElementById('map_addresses'),mapOptions);

  jQuery(arr).each(function(i){
     if(typeof arr[i] != 'undefined'){  
      var latlng = new google.maps.LatLng(arr[i][0],arr[i][1]);
            geocoder.geocode(
                {'latLng': latlng},
                function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                  bounds.extend(results[0].geometry.location);
                  map.fitBounds(bounds);

                   marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    icon: icon ,
                    animation: google.maps.Animation.DROP,
                  });
                  //google.maps.event.addListener(marker, 'click', toggleBounce);
                  markersArray.push(marker);
                } else {
                  alert('Geocode was not successful for the following reason: '
                    + status);
                }
              }
          );
     }
  });
}
google.maps.event.addDomListener(window, 'load', initialize);

hope this will help you

Upvotes: 0

Related Questions