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

相關問題