下面的代码可以正常工作。但它没有使用初始化的标准函数,这就是为什么我无法使用在线示例为 map 上的每个地址创建自定义信息窗口。
我必须如何定义它才能使其正常工作?
var address = <?php echo json_encode($adr); ?>; //define address array here
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 10
});
var geocoder = new google.maps.Geocoder();
var image = 'http://www.site.com/images/hi.png';
var nbAddresses = address.length;
for (var i=0;i<nbAddresses;i++){
geocoder.geocode({
'address': address[i]
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map,
icon: image,
title:"Hello"
});
map.setCenter(results[0].geometry.location);
}
});
}
最佳答案
这是应该完成这项工作的 Javascript
脚本:
geocoder.geocode({
'address': add
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
mark = new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
map.setCenter(results[0].geometry.location);
setInfoWindow();
}
});
function setInfoWindow() {
google.maps.event.addListener(mark, 'click', function(event) {
var iwindow = new google.maps.InfoWindow();
iwindow.setContent(event.latLng.lat() + "," + event.latLng.lng());
iwindow.open(map, this);
});
}
更新: 使用循环演示如何在地理编码成功后将标题值分配给制作者-
这里的关键是用于获取地址的循环变量在子函数的范围内不可用,因此您需要创建一个全局变量并递增它以从标题数组中获取新值。
关于google-maps - 设置标记的 InfoWindows,并循环进行地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14119097/