我正在尝试构建一个Web应用程序,它从服务器中提取数据并将多个标记添加到来自JSON响应的Google地图中。这是JSON响应的样子:从json添加多个标记到Google地图
{"key":[{"Latitude":"60.186518","Longitude":"24.950575"}...]}
我试图实现从this post代码,但我无法得到它的工作。
这是我到目前为止的代码:
function success(position) {
var mapcanvas = document.createElement('div');
mapcanvas.id = 'mapcontainer';
mapcanvas.style.height = '600px';
document.querySelector('article').appendChild(mapcanvas);
var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var options = {
zoom: 15,
center: coords,
panControl: false,
zoomControl: false,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
overviewMapControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("mapcontainer"), options);
var marker = new google.maps.Marker({
position: coords,
map: map,
title:"You are here!"});
function getLocations() {
$.getJSON("http://myurl.com/jsonresponse", function (json) {
var location;
$.each(json.key, function (i, item) {
addMarker(item.Latitude,item.Longitude);
});
});
}
function addMarker(lat,lng) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
});
markersArray.push(marker);
}
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
error('Geo Location is not supported');
}
什么不工作?如果你调试,你会得到哪些错误? – SativaNL 2013-03-26 10:03:11
如果你有很多标记,我建议你使用KML文件... – grigno 2013-03-26 10:12:19
当前我有36个标记。我似乎没有得到任何错误。我认为addmarker函数有问题,但我不确定。 – user2210792 2013-03-26 10:24:43