【发布时间】:2015-06-10 08:52:56
【问题描述】:
我正在使用 Google Maps JavaScript API v3 生成具有多个位置/标记的地图。我只有这些位置的地址,没有坐标,所以我使用 Geocoding API 来获取坐标。
我终于让谷歌的地理编码工作了,所以位置标记出现在它们应该在的位置。但是,相同的内容显示在每个 InfoWindow 中。我似乎无法将位置数组传递给地理编码函数。 (顺便说一句,我还尝试为地理编码结果创建一个变量,并将 infoWindow 函数移动到地理编码函数之外,但我也无法做到这一点。)
我已经尝试了一百种不同的方法。我希望别人能看到我看不到的东西。
var locations = [
['Location 1 Name', 'Location 1 Address', 'Location 1 URL'],
['Location 2 Name', 'Location 2 Address', 'Location 2 URL'],
['Location 3 Name', 'Location 3 Address', 'Location 3 URL']
];
geocoder = new google.maps.Geocoder();
for (i = 0; i < locations.length; i++) {
title = locations[i][0];
address = locations[i][1];
url = locations[i][2];
geocoder.geocode({ 'address' : locations[i][1] }, function(results, status) {
marker = new google.maps.Marker({
icon: 'marker_blue.png',
map: map,
position: results[0].geometry.location,
title: title,
animation: google.maps.Animation.DROP,
address: address,
url: url
})
infoWindow(marker, map, title, address, url);
})
}
function infoWindow(marker, map, title, address, url) {
google.maps.event.addListener(marker, 'click', function() {
var html = "<div><h3>" + title + "</h3><p>" + address + "<br></div><a href='" + url + "'>View location</a></p></div>";
iw = new google.maps.InfoWindow({ content : html, maxWidth : 350});
iw.open(map,marker);
});
}
【问题讨论】:
标签: javascript google-maps google-maps-api-3 google-geocoder google-geocoding-api