[英]Javascript google maps foreach infowindows
Hey someonse knows why i dind't get my address in the content of my infowindows of the markers ? 嘿,有人知道为什么我在标记的信息窗口的内容中没有得到我的地址吗? in my loop before the geocoderstatus i have my address info but after the geocoderstatus.ok i have undefined or only the last address of my array. 在我的循环中,在geocoderstatus之前,我有我的地址信息,但是在geocoderstatus.ok之后,我未定义或只有我数组的最后一个地址。
function geocodeAddress(resultsMap) {
// var address = document.getElementById('address').value;
var address = ["Brussel","Oostende","Brugge","Gent","Luik"];
for(var i = 0; i < address.length;i++)
{
//new
var geocoder = new google.maps.Geocoder();
var geooptions = {
address: address[i]
}
geocoder.geocode(geooptions, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
// resultsMap.setCenter(results[0].geometry.location)
addMarker(resultsMap,address[i], results[0].geometry.location);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
}
function addMarker(map,item,location) {
console.log(item);
var marker= new google.maps.Marker({
map: map,
position: location, //results[0].geometry.location,
title: item
});
var infowindow = new google.maps.InfoWindow({
content: item
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
I have found the solution it works only with a jquery.each. 我发现了仅适用于jquery.each的解决方案。 Wierd because its just the same as a for loop. 奇怪,因为它与for循环相同。
for(var item in address) isn't working either because then i get only the index and not the value. for(地址中的var项)也不起作用,因为然后我仅获得索引而不是值。
this fix the issue $(address).each(function (index,item) 这解决了问题$(address).each(function(index,item)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.