简体   繁体   English

Google Map Infowindow无法从var加载内容

[英]Google map Infowindow not loading content from var

I need help getting the infowindow to display the values from locations[i][0] 我需要帮助以获取信息窗口以显示位置中的值[i] [0]

locations[i][1] is working just fine for the address value but i would like to be able to set a custom title for every marker in the infowindow from the var "locations". locations [i] [1]可以很好地处理地址值,但是我希望能够从var“ locations”为信息窗口中的每个标记设置自定义标题。

I can define a var to set the content and info window works that way but its the same for every marker and not pulling from the var "locations" 我可以定义一个变量来设置内容和信息窗口的工作方式,但每个标记都相同,而不是从变量“位置”中提取

Any help is appreciated! 任何帮助表示赞赏!

function initialize() {


geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 10,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(
document.getElementById("map_canvas"),
myOptions);
setMarkers(map, locations);
}

var locations = [
['Bondi Beach', '798 9th Ave, New York, NY', 4],
['Coogee Beach', '42 E 29th St, New York, NY', 5],
['Cronulla Beach', '56 W 25th St, New York, NY', 3],

];



function setMarkers(map, locations) {

var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
 var marker, i; 

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


   geocoder.geocode({'address': locations[i][1]}, function(results, status) { 

    marker = new google.maps.Marker({
    position: results[0].geometry.location,
    map: map,


    });


  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {

      infowindow.open(map, marker);
      infowindow.setContent(locations[i][0]);
    }
  })(marker, i));

bounds.extend(marker.getPosition());


map.fitBounds(bounds);

})
  }
}

What's happening is the click event listener is taking i = 3 (the last value of i after the loop ends) so no InfoWindow appeared because of this undefined status. 发生的事情是单击事件侦听器将i = 3 (循环结束后i的最后一个值)设为i,因此由于此未定义状态而没有出现InfoWindow。

Because the geocoder was introduced, the outer for loop finishes before the click listeners get their functions assigned to them. 由于引入了地址解析器,因此外部for循环在点击侦听器分配给它们的功能之前完成。 So, another function scope wrapper, that is outside the geocoding, is needed to keep the right value of i in the listeners. 因此,需要在地理编码之外的另一个函数范围包装器,以将i的正确值保留在侦听器中。

function setMarkers(map, locations) {

  var infowindow = new google.maps.InfoWindow();
  var bounds = new google.maps.LatLngBounds();
  var marker, i; 

  //CHANGED REMOVED EQUALS SIGN
  for (var i=0; i<locations.length; i++) { 

   //ADDED
   (function(i) {
   geocoder.geocode({'address': locations[i][1]}, function(results, status) { 

     marker = new google.maps.Marker({
       position: results[0].geometry.location,

      //REMOVED COMMA
       map: map
     });

     google.maps.event.addListener(marker, 'click', (function(marker, i) {
       return function() {

         //CHANGED ORDER
         infowindow.setContent(locations[i][0]);
         infowindow.open(map, marker);
       }
     })(marker, i));

     bounds.extend(marker.getPosition());
     map.fitBounds(bounds);
     });

    // ADDED
    })(i);
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM