在标记点击上显示infowindow内的数组内容

时间:2015-06-19 13:10:27

标签: javascript google-maps infowindow geocode google-geocoding-api

我有一个api端点,它返回一个地址数组和地址的一些价格信息,但它没有返回lat / lang,所以我使用geocode来显示地图上地址的标记并显示infowindow on每个标记点击。现在我希望infowindow的内容是从端点返回的数组中的地址的价格。希望它清楚到目前为止。现在问题是每次我点击标记,infowindow只显示每个infowindow上的数组的最后(相同)价格,但不显示与该地址相关的价格。请参阅下面的代码(或检查fiddlejs => https://jsfiddle.net/puLxak1k/7/)。它可能是我试图在asyn回调中调用同步,所以我尝试使用$ q.promise并解决初始api调用并尝试在.then内调用geocode.geocoder,但没有运气..

    <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);

1 个答案:

答案 0 :(得分: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);