Javascript 实时gps跟踪谷歌地图
我正在构建一个应用程序,它必须在地图上显示多个车辆,并更新其当前位置。我已经在地图上创建并放置了标记,但是我不知道如何更新每个标记的位置 为了更新位置,我正在使用google内置的setPosition方法,但是我一直得到的setPosition不是一个函数错误 我从外部来源获得车辆的gps坐标,但若有帮助,我已经提供了一个数据样本Javascript 实时gps跟踪谷歌地图,javascript,html,arrays,google-maps-api-3,Javascript,Html,Arrays,Google Maps Api 3,我正在构建一个应用程序,它必须在地图上显示多个车辆,并更新其当前位置。我已经在地图上创建并放置了标记,但是我不知道如何更新每个标记的位置 为了更新位置,我正在使用google内置的setPosition方法,但是我一直得到的setPosition不是一个函数错误 我从外部来源获得车辆的gps坐标,但若有帮助,我已经提供了一个数据样本 var sampleData= [{"ObjectID":"001","Latitude":51.418611111111,"Longitude":-0.05716
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);
});
}
});
}什么是标记[索引]['ObjectID']?您应该在标记对象上调用setPosition。它是标记对象本身。如果在循环中调用alertmarkers[index]['ObjectID'],则会得到001002003.001002003不是标记对象!也许是你对马克物体的引用。对不起,这是对这个物体的引用。但是我不能使用对对象的引用来调用对象吗?如果不是的话,我怎么可能调用循环中的每个对象,这样函数就可以将正确的标记放在正确的位置上呢?setPositionsmarkers[index]?