【发布时间】:2015-08-15 01:58:40
【问题描述】:
我无法根据纬度和经度在我的 Google 地图上添加多个标记。我正在从本地主机对我的 JSON 进行 AJAX 调用。 这是 JavaScript 代码:
var map;
var Latitude, Longitude;
var markersArray = [];
function initialize() {
var myLatlng1 = new google.maps.LatLng(20.2700, 85.8400);
var mapOptions = {
zoom: 6,
center: myLatlng1,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map'),
mapOptions);
//if (navigator.geolocation) {
// navigator.geolocation.getCurrentPosition(function (position) {
// initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
// map.setCenter(initialLocation);
// });
//}
initialLocation = new google.maps.LatLng(20.2700, 85.8400);
map.setCenter(initialLocation);
var marker = new google.maps.Marker({
position: myLatlng1,
});
marker.setMap(map);
var marker = new google.maps.Marker({ position: initialLocation, map: map, title: "You are here!" });
getLocations();
//PlotMarker(Latitude,Longitude);
};
function getLocations() {
$.ajax({
type: "GET",
url: "http://localhost:44149/api/values",
dataType: "json",
success: function (json) {
$.each(json, function (key, value) {
//console.log("Key : " +key + " Value : "+value.Longitude );
PlotMarker(value.Latitude, value.Longitude);
});
},
error: function (err) {
console.log(err);
}
});
}
function PlotMarker(Lat, Long) {
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(Lat, Long),
map: map,
draggable: false,
animation: google.maps.Animation.DROP
});
markersArray.push(marker1);
}
//google.maps.event.addDomListener(window, "load", initialize);
注意:我的初始位置的标记正在工作,但没有添加多个标记。 你能发现错误吗?您的帮助将不胜感激。 谢谢。
【问题讨论】:
标签: javascript ajax json google-maps google-maps-api-3