我正在构建一个应用程序,它必须在地图上显示多个车辆并在间隔时间更新其当前位置。我已设法在地图上创建并放置标记,但我无法弄清楚如何更新每个标记位置。
更新我使用谷歌内置的setPoistion方法的位置,但我一直得到(setPosition不是函数错误)
我从外部来源获取车辆的GPS坐标,但如果有帮助,我已经包含了一些数据样本。
var sampleData= [{"ObjectID":"001","Latitude":51.418611111111,"Longitude":-0.057166666666667},
{"ObjectID":"002","Latitude":51.54175,"Longitude":-0.35825},
{"ObjectID":"003","Latitude":51.544666666667,"Longitude":5.9852777777778}];
function initialize() {
$.post("../test/test.php", function(data){
var markers = jQuery.parseJSON(data);
var myMarkers = new Array();
var latlng = new google.maps.LatLng(51.505568, -0.118421);
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
// Add markers to array & add markers to the map
for (index in markers){
myMarkers[ markers[index]['ObjectID']] = addMarker(map, markers[index]);
};
// Update markers location
setTimeout(function(){
for(index in markers){
setPositions(markers[index]['ObjectID']);
}
},10*1000);
// Update markers position function
function setPositions(data){
// Im using default location as the vehicles are not moving at current state.
data.setPosition(new google.maps.LatLng(51.505568, -0.118421));
}
function addMarker(map,data){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.Latitude,data.Longitude),
map: map,
title: data.ObjectID
});
var infowindow = new google.maps.InfoWindow({
content: data.ObjectID
});
google.maps.event.addListener(marker, "click", function(){
infowindow.open(map,marker);
});
}
});
}