在谷歌地图上显示多个标记我有以下脚本来使用谷歌地图API,我必须创建一个具有多个标记(气球形状的图标指向某个东西)的地图,我需要每个那些标记指向地图的不同区域(即不同的坐标),我该怎么做? 注意:我没有经纬度来放置标记的区域,而是具有放置标记的区域的地址。通过提供地址
下面给出了在Google地图上显示多个标记的代码,但它不能按预期工作!
var map = null;
var geocoder = null;
function initializeNew() {
var allAddress = document.getElementById("HiddenField1").value;
var testary = new Array();
testary = allAddress.split("|");
if (GBrowserIsCompatible()) {
//calls map to appear inside <div> with id, "map_canvas"
map = new GMap2(document.getElementById("map_canvas"));
//adds zoom and arrow controls to map
//map.addControl(new GSmallMapControl());
var marker = null;
var i = 0;
for (i = 0; i < testary.length; i++) {
address = testary[i];
geocoder = new GClientGeocoder();
//converts a street address into geocode for the api
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
map.setCenter(point, 13);
marker = new GMarker(point);
map.addOverlay(marker);
map.setMapType(G_HYBRID_MAP);
//adds your own marker title and description in html
marker.openInfoWindowHtml("<p class='para1bold' style=\"font-weight: bold;\">Address</p><p class='para1'>" + address + "</p>");
//Add click event on push pin to open info window on click of the icon
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml("<p class='para1bold' style=\"font-weight: bold;\">Address</p><p class='para1'>" + address + "</p>");
});
//Provides map,satellite,hybrid and terrain views in the map along with zoom control
map.setUIToDefault();
}
}
)
}
}
}
我已经将一组项目的地址存储在一个数组中,并循环显示地图中的标记。这仅显示数组中最后一个地址的一个标记。请更正此代码或提供新代码,以通过提供地址在Google地图中显示多个标记。
您是否尝试打印出地理编码的结果?所有不同的经纬度? – jebberwocky 2010-11-01 10:14:51
当我在geocoder.getLatLng方法的else部分中输出地址变量(数组中的项)时,它显示数组中的最后一项(最后一个地址)。我认为它在Google地图中的相同位置显示多个标记!任何人都可以指导我纠正代码! – banupriya 2010-11-01 10:45:31
也许您提供给Google Geocoder webervice的地址太相似了,这可能会导致相同的经过长时间的结果。 – Vishwanath 2010-11-13 14:13:27