0

我正在创建一个谷歌地图以在其上显示多个位置。我期待数据来自任何服务源(这里我暂时对其进行了硬编码)。最后一个位置的内容与所有标记绑定。我已经完成了这里给出的所有讨论,但无法得到解决方案。

这是我的代码:

var LocationArray = [
                 [ 26.864981300000000000, 80.962066000000050000],
                 [12.971598700000000000,77.594562699999980000],
                 [19.017614700000000000,72.856164400000010000],
                 [ 28.635308000000000000, 77.224960000000010000],
                 [ 13.060422000000000000, 80.249583000000030000],
                 [ 22.572646000000000000, 88.363894999999950000]
               ];
var mapOptions = {
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP    
  }
function showMultiplePostionOnMap(mapOptions,LocationArray){    
    var MapObjectForShowMultiplePostionOnMap =  new google.maps.Map(document.getElementById("MapAreaNode"), mapOptions);    
    var infowindow= new google.maps.InfoWindow();   
     for( var i = 0; i < LocationArray.length; i++){        
        var marker=new google.maps.Marker({
            icon:image,
            animation: google.maps.Animation.DROP           
        });
        marker.setMap(MapObjectForShowMultiplePostionOnMap);
        marker.setPosition(new google.maps.LatLng(LocationArray[i][0], LocationArray[i][1]));   
        if(parseInt((LocationArray.length)/2)==i)
        {
            MapObjectForShowMultiplePostionOnMap.setCenter( new google.maps.LatLng(LocationArray[i][0], LocationArray[i][1]));
        }           
        var infocor = new google.maps.LatLng(LocationArray[i][0], LocationArray[i][1]);     
        var add;
        geocoder.geocode({'location':infocor}, function(results, status)
        {       
            if(status == google.maps.GeocoderStatus.OK) {
            add=results[0].formatted_address;
        }
        });     
        google.maps.event.addListener(marker, 'click', (function(marker,i) {        
            return function(){  
                infowindow.setContent(add);
                infowindow.open(MapObjectForShowMultiplePostionOnMap,marker);
            }
        })(marker,i));                         
     }


}

我认为这是一个重复的问题,但我已经尝试了很多但无法得到任何解决方案。

4

1 回答 1

1

您没有包含add在自执行功能中,add将被每个地理编码响应覆盖。

可能的解决方案(将- 属性添加add到标记):

var infocor = new google.maps.LatLng(LocationArray[i][0], LocationArray[i][1]); 

(function(marker,infocor){
  geocoder.geocode({'location':infocor}, function(results, status) {  
      if(status == google.maps.GeocoderStatus.OK) {
      marker.add=results[0].formatted_address;
    }
  });  
})(marker,infocor);

google.maps.event.addListener(marker, 'click', function() {  
  if(this.add) { //geocoding was successfull   
    infowindow.setContent(this.add);
    infowindow.open(MapObjectForShowMultiplePostionOnMap,this);
  }
});
于 2013-10-01T12:00:23.577 回答