开发者

Google Maps markers show same info

开发者 https://www.devze.com 2023-04-13 00:22 出处:网络
I know that similar questions have been asked before but the provided answers don\'t solve my problem. I have a loop in which I iterate through places, create markers and add eventlisteners. When I cl

I know that similar questions have been asked before but the provided answers don't solve my problem. I have a loop in which I iterate through places, create markers and add eventlisteners. When I click on any of the markers afterwards they all show the same information.

  var geocoder;
 var map;
function initialize() {

// Load places from xml file
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  开发者_如何学C}
xmlhttp.open("GET","person_city_clause.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

var text =
xmlDoc.getElementsByTagName("text");

var places = [];
var descriptions = [];

for (var i=3; i<text.length; i++)
{
    places.push(text[i].childNodes[0].nodeValue); // place
    descriptions[places[i]] = descriptions.push(text[i-2].childNodes[0].nodeValue); // person
    i=i+3;
}

// Create inital Google Map
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
  zoom: 2,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);



// fill map with place markers
geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
var marker = [];

for (var i=0; i<places.length; i++)
{
    geocoder.geocode( { 'address': places[i]}, function(results, status) 
    {

        if (status == google.maps.GeocoderStatus.OK) 
        {
            marker[i] = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location});

            listenMarker(marker[i], results[0].formatted_address );
        }

    })  
}

function listenMarker(marker, place)
{
    google.maps.event.addListener(marker, 'click', function(){
            infowindow.setContent("" + place);
            infowindow.open(map, this);});
            }

}

edit: when I do alert(i) after geocoder.geocode I always get 32 (the size of the places array).


You need an array for the infowindows aswell, if you do not do this the infowindow variable gets overridden by the next marker..

The content is the same for all of them..

EDIT: Just make another global array:

    var locations = [];

    ..code blabla
    if(status == google.maps.GeocoderStatus.OK)
    {
        for(int k = 0; k<results.length;k++{
            locations.push = results[k].geometry.location;
        }
    }

You can loop through this array using this:

for(int i=0; i<locations.length;i++)
{

}


Try setting a local var in the context of the function, that creates the event function:

...

var index = i;

if (status == google.maps.GeocoderStatus.OK) {

  ...

  infowindow.setContent("" + index);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号