我尝试使用这种方式(示例)使用 Gmap V2 添加/自定义标记:
for (var i in table)
{
var myvar = table[i]['text'] ;
var myaddress = table[i]['address'] ;
geocoder.getLatLng(
myaddress ,
function(point) {
alert(myvar) ; // here myvar is wrong
// ... adding customer markers ...
}
});
}
在这个示例中,我得到了表中每个条目的优点,但是 myvar 是错误的,在每次调用中:myvar 保持等于表的最后一个条目...
geocoder.getLatLng 是异步函数,是因为那个吗?
编辑: 谢谢你的回复。但是当我使用循环时,我遇到了这个问题,例如:
var address = 'somewhere';
for (i = 0 ; i < 3 ; i++)
{
geocoder.getLatLng(
address,
function(point) {
if (point) {
alert(i);
}
});
}
点总是等于 3 !
最佳答案
这就是我所做的。
if ($('#map_canvas').length != 0) {
var marker= new GIcon(title);
marker.image = '/images/icon.png';
marker.iconSize = new GSize(139,64);
marker.iconAnchor = new GPoint(0, 64);
marker.name = title ;
markerOptions = { icon:marker };
map = new GMap2(document.getElementById("map_canvas"));
geocoder = new GClientGeocoder();
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
map.setCenter(point, 14);
map.addOverlay(new GMarker(point, markerOptions));
}
}
);
}
});
如果这对您没有帮助,您可以查看: http://code.google.com/intl/da/apis/maps/documentation/javascript/overlays.html#Icons
关于javascript - Google Map API v2 - GeoCoder - 如何自定义标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4040750/