我正在使用Google Maps API V2(我知道,但我正在编辑当前代码,因此必须使用V2)。我的所有标记都指向正确的位置,但只有一个问题让我接近拉头发。
问题是只有一个标记正确显示,但信息窗口只有一个内容。即信息窗口未被更新。它们弹出正确,但它们在所有这些中都有相同的信息。
我在其他一些论坛上看到了这个问题并尝试了解决方案,但无济于事。
以下是从我的php脚本中获取json编码的PHP字符串然后将其循环以在google地图上显示标记的代码。
function showAddress(address) {
var response=$.ajax({
type: "POST",
url: "<?php echo WEBROOT; ?>/index/ajaxMaps",
data: {address : escape(address)},
cache: false,
async:false,
dataType: "json",
success: function (html) {
for(i in html){
if (geocoder) {
geocoder.getLatLng(
unescape(html[i].businessAddress),
function(point) {
if(!point) {
//alert(address + " not found");
} else {
map.setCenter(point, 11);
string='<div style="height:100px; background:green; color:white; padding:5px; border-radius:0.5em; width:auto"><strong>Company name : </strong>'+ html[i].businessName;
string+='<br/><strong>Adress : </strong>'+ html[i].businessAddress;
string+='<br/><strong>Website : </strong>'+'<a style="color:white !important" href="'+html[i].businessWebsite+'" >'+'Visit our website'+'</a></div>';
var marker = createMarker(point, string);
map.addOverlay(marker);
//map.addMapType(G_PHYSICAL_MAP);
map.addControl(new GLargeMapControl());
// As this is user-generated content, we display it as
// text rather than HTML to reduce XSS vulnerabilities.
marker.openInfoWindowHtml(string);
//$('.map_enlarged').trigger('click');
}
}
);
}
}
}
});
$('.map_enlarged').trigger('click');
}
现在添加标记的功能:
function createMarker(point,html) {
// Create our "tiny" marker icon
var blueIcon = new GIcon(G_DEFAULT_ICON);
blueIcon.image = "<?php echo WEBROOT; ?>images/marker.png";
// Set up our GMarkerOptions object
markerOptions = { icon:blueIcon };
var marker = new GMarker(point,markerOptions);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
我知道变量名称“字符串”未更新。但我没有看到原因。我已将其追溯到循环,但看不出它为什么不应该更新。
请记住,我已经挖掘了大部分上述代码,并且如果可以,请指出我的代码中的错误。提供其他链接可能没有用,因为我可能已经看过它们了。
我将很感激解决方案。
答案 0 :(得分:1)
“i”需要另一个函数闭包(关闭地理编码器的输入参数)。
这个example解决了这个问题,但没有解决查询限制问题(你真的不应该动态地对大量地址进行地理编码,将它们离线地编码并存储生成的坐标)。