1

我得到了一个api端点,它返回地址数组和地址的一些价格信息,但它不返回纬度/经度,所以我使用地理编码显示标记在地图上显示地址,并在每个标记点击上显示infowindow。现在我想让infowindow的内容成为从端点返回的数组中地址的价格。希望迄今为止清楚。现在问题出在我每次点击标记时,infowindow只显示每个infowindow上数组的最后(相同)价格,而不是与该地址相关的价格。请参阅下面的代码(或登入fiddlejs =>https://jsfiddle.net/puLxak1k/7/)。这可能是我试图调用同步在ASYN回调,所以我尝试使用$ q.promise和解决初始API调用和打过电话里面。然后geocode.geocoder,但没有运气尚未..在infowindow中显示来自Array内的内容点击标记

<div id="map-canvas"></div> 
    function initialize() { 
    var chicago = new google.maps.LatLng(41.850033, -87.6500523); 
    var mapOptions = { 
     zoom: 7, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: chicago 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    geocoder = new google.maps.Geocoder() 
    var someArray = [{price: 10, address: 'cv64ad'}, {price: 20, address: 'cv59bp'}] 
    for (var i = 0; i < someArray.length; i++) { 
     var home = someArray[i] 
     console.log(home.address) 

     geocoder.geocode({'address': home.address}, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 
      map.setZoom(12); 
      for (var i in results) { 
      var marker = new google.maps.Marker({ 
       position: new google.maps.LatLng(results[i].geometry.location.A, results[i].geometry.location.F), 
       map: map 
      }) 
      google.maps.event.addListener(marker, 'click', someCallback(home)) 
      infowindow = new google.maps.InfoWindow(); 
      function someCallback(home) { 
       return function (e) { 
       if (infowindow != null) { 
        infowindow.close(); 
       } 
       infowindow.setContent('<div class="infoheading">' + home.price + '</div>'); 
       infowindow.open(map, this); 
       } 
      } 
      } 
     } 
     }) 
    } 
    } 

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

回答

3

像这样: https://jsfiddle.net/jvxppeap/2/

诀窍是:让一个数组保存标记对象。 然后,在addListener(标记,点击'...')中检查点击了哪些标记;查看我的代码中的“indexOf” 使用该索引,您可以阅读someArray的正确项目,可以添加你想要有任何信息。

var markers = []; 
var map; 
var infowindow; 
function initialize() { 
    var chicago = new google.maps.LatLng(41.850033, -87.6500523); 
    var mapOptions = { 
    zoom: 7, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    center: chicago 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    geocoder = new google.maps.Geocoder(); 
    var someArray = [{price: 10, address: 'cv64ad'}, {price: 20, address: 'cv59bp'}]; 

    for (var i = 0; i < someArray.length; i++) { 
    var home = someArray[i]; 
    console.log(home.address); 

    geocoder.geocode({'address': home.address}, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     map.setZoom(12); 
     for (var i in results) { 
      var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(results[i].geometry.location.A, results[i].geometry.location.F), 
      map: map 
      }); 
      // push the marker on the markers array 
      markers.push(marker); 
      google.maps.event.addListener(marker, 'click', function (home) { 
       if (infowindow != null) { 
       infowindow.close(); 
       } 
       // now, we want to know which of the markers we're talking about 
       var index = markers.indexOf(this); 
       infowindow.setContent('<div class="infoheading">' + someArray[index].price + '</div>'); 
       infowindow.open(map, this); 
      }); 
      infowindow = new google.maps.InfoWindow(); 
     } 
     } 
    }) 
    } 
} 
google.maps.event.addDomListener(window, 'load', initialize); 
+0

谢谢灵光,它帮助并解决了....我更对http://stackoverflow.com/questions/17684153/angular-js-pass-data倾斜 - 从异步服务到范围 –

+0

这工作完美。感谢您提供完整的代码和解释注释中的函数 – user3411192

相关问题