我使用Google map api v3
绘制位置数组的标记。我的脚本是
function OnSuccess(response) {
var markers = response.d.split('^^');
var latlng = new google.maps.LatLng(51.474634, -0.195791);
var mapOptions1 = {
zoom: 14,
center: latlng
}
var geocoder = new google.maps.Geocoder();
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions1);
for (i = 0; i < markers.length; i++) {
var data = markers[i];
geocoder.geocode({ 'address': data }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: data
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
(function (marker, data) {
// Attaching a click event to the current marker
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data);
infoWindow.open(map, marker);
});
})(marker, data);
}
其中markers
变量带来了正确的数据,我的测试数据是5个元素的数组
16A The Parade,Claygate,ESHER,KT10 0NU
同样进入数组markers
但是它只绘制了两个标记。这可能有什么问题
解决方案
我正在进入彼此非常接近的位置,例如1和2点,它们给出相同的latlong
因此标记为一个地方。我找到了latlong
here。谢谢你的答案顺便说一句:)
答案 0 :(得分:1)
在page.aspx中。插入标记<div id="map-canvas" ></div>
查看源page.aspx并将脚本插入其中&gt;
var lis_marker = new Array();
for (i = 0; i < markers.length; i++) {
var data = markers[i];
geocoder.geocode({ 'address': data }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
lis_marker[i] = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: data
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
与您的代码存在差异:
1:var lis_marker = new Array();
2:lis_marker[i] = new google.maps.Marker({...});