所以,我试图每五秒更新一次标记/用户的位置,以便用户实时更新他们的位置。我可以让当前位置和坐标以正确的间隔更新,但我希望谷歌地图上的标记更新,我不明白在谷歌地图代码中的哪里放置我的坐标以便实时更新?
var x=document.getElementById("demo");
function startGeolocation() {
var options;
navigator.geolocation.getCurrentPosition(geoSuccess, options);
setTimeout(startGeolocation, 1000);
}
function geoSuccess(position) {
var gpsPosition = position;
var coordinates = gpsPosition.coords;
myLat = coordinates.latitude;
myLong = coordinates.longitude;
//x.innerHTML="Latitude: " + myLat +
//"<br>Longitude: " + myLong;
}
var map;
function initialize() {
var mapOptions = {
zoom: 18,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position, options) {
//trying to pass in myLat and myLong from the live update above
var pos = new google.maps.LatLng(position.coords.myLat,
position.coords.myLong);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'You are here'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);