用户位置更改时获取更新

你还可以定期收到用户位置的更新; 例如,当他们在使用移动设备时四处移动。随着时间的推移,位置跟踪可能非常敏感,因此请务必提前向用户说明你为什么要请求此权限以及如何使用这些数据。

if (navigator.geolocation) {
    //after the user indicates that they want to turn on continuous location-tracking
    var watchId = navigator.geolocation.watchPosition(updateLocation, geolocationFailure);
} else {
    console.log("Geolocation is not supported by this browser.");
}

var updateLocation = function(position) {
    console.log("New position at: " + position.coords.latitude + ", " + position.coords.longitude);
};

要关闭持续更新:

navigator.geolocation.clearWatch(watchId);