几个月前我一直在做类似的事情.我记得有同样的问题.也许这会有所帮助.
$(document).ready(function(){
initLocationProcedure();
}
function initLocationProcedure() {
map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom : 17
});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayAndWatch, locError, {
enableHighAccuracy : true,
timeout : 60000,
maximumAge : 0
});
} else {
alert("Your phone does not support the Geolocation API");
}
}
function displayAndWatch(position) {
// set current position
setUserLocation(position);
// watch position
watchCurrentPosition();
}
function setUserLocation(pos) {
// marker for userLocation
userLocation = new google.maps.Marker({
map : map,
position : new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude),
title : "You are here",
icon : "../img/user-location.svg",
// scroll to userLocation
map.panTo(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
});
这是你在寻找的
function watchCurrentPosition() {
var positionTimer = navigator.geolocation.watchPosition(function(position) {
setMarkerPosition(userLocation, position);
map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
});
}
function setMarkerPosition(marker, position) {
marker.setPosition(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
console.log(position);
}