为什么我的 PhoneGap 定位函数 return 多次定位?
Why does my PhoneGap location function return the position multiple times?
我有这个功能可以得到更准确的 GPS 结果:
navigator.geolocation.getAccurateCurrentPosition = function (geolocationSuccess, geolocationError, geoprogress, options) {
var lastCheckedPosition,
locationEventCount = 0,
watchID,
timerID;
options = options || {};
var checkLocation = function (position) {
lastCheckedPosition = position;
locationEventCount = locationEventCount + 1;
// We ignore the first event unless it's the only one received because some devices seem to send a cached
// location even when maxaimumAge is set to zero
if ((position.coords.accuracy <= options.desiredAccuracy) && (locationEventCount > 1)) {
clearTimeout(timerID);
navigator.geolocation.clearWatch(watchID);
foundPosition(position);
} else {
geoprogress(position);
}
};
var stopTrying = function () {
navigator.geolocation.clearWatch(watchID);
foundPosition(lastCheckedPosition);
};
var onError = function (error) {
clearTimeout(timerID);
navigator.geolocation.clearWatch(watchID);
geolocationError(error);
};
var foundPosition = function (position) {
geolocationSuccess(position);
};
if (!options.maxWait) options.maxWait = 10000; // Default 10 seconds
if (!options.desiredAccuracy) options.desiredAccuracy = 20; // Default 20 meters
if (!options.timeout) options.timeout = options.maxWait; // Default to maxWait
options.maximumAge = 0; // Force current locations only
options.enableHighAccuracy = true; // Force high accuracy (otherwise, why are you using this function?)
watchID = navigator.geolocation.watchPosition(checkLocation, onError, options);
timerID = setTimeout(stopTrying, options.maxWait); // Set a timeout that will abandon the location loop
};
然后我打电话给:
navigator.geolocation.getAccurateCurrentPosition(onSuccess, onError, onProgress, {desiredAccuracy:20, maxWait:15000});
当它找到一个位置时,它会触发 geolocationSuccess(position);
(上述功能成功。问题是在我的 PhoneGap 应用程序中,它似乎经常多次调用它。有时 4-5同一秒内的次数。
为什么会这样?我只初始化它一次,它不在任何循环中。
脚本作者在 github readme 上回答了您的问题(重点是我的):
A better way to do this is to use navigator.geolocation.watchPosition(). This method will do a callback every time the location changes or every time the device improves the accuracy (based on my observations). In my own testing with a freshly booted device, it will take between 2 and 6 callbacks to get to something highly accurate. This led me to write this very simple JavaScript function that uses watchPosition() in combination with a simple timer.
所以作者在这个实现中使用的是geolocation watchPosition()
方法。
如果位置或精度发生变化,watchPosition()
方法的回调可能会被调用多次。参见documentation(重点又是我的):
If the position data changes (either by device movement or if more
accurate geo information arrives), you can set up a callback function
that is called with that updated position information. This is done
using the watchPosition() function, which has the same input
parameters as getCurrentPosition(). The callback function is called
multiple times, allowing the browser to either update your location as
you move, or provide a more accurate location as different techniques
are used to geolocate you. The error callback function, which is
optional just as it is for getCurrentPosition(), can be called
repeatedly.
我有这个功能可以得到更准确的 GPS 结果:
navigator.geolocation.getAccurateCurrentPosition = function (geolocationSuccess, geolocationError, geoprogress, options) {
var lastCheckedPosition,
locationEventCount = 0,
watchID,
timerID;
options = options || {};
var checkLocation = function (position) {
lastCheckedPosition = position;
locationEventCount = locationEventCount + 1;
// We ignore the first event unless it's the only one received because some devices seem to send a cached
// location even when maxaimumAge is set to zero
if ((position.coords.accuracy <= options.desiredAccuracy) && (locationEventCount > 1)) {
clearTimeout(timerID);
navigator.geolocation.clearWatch(watchID);
foundPosition(position);
} else {
geoprogress(position);
}
};
var stopTrying = function () {
navigator.geolocation.clearWatch(watchID);
foundPosition(lastCheckedPosition);
};
var onError = function (error) {
clearTimeout(timerID);
navigator.geolocation.clearWatch(watchID);
geolocationError(error);
};
var foundPosition = function (position) {
geolocationSuccess(position);
};
if (!options.maxWait) options.maxWait = 10000; // Default 10 seconds
if (!options.desiredAccuracy) options.desiredAccuracy = 20; // Default 20 meters
if (!options.timeout) options.timeout = options.maxWait; // Default to maxWait
options.maximumAge = 0; // Force current locations only
options.enableHighAccuracy = true; // Force high accuracy (otherwise, why are you using this function?)
watchID = navigator.geolocation.watchPosition(checkLocation, onError, options);
timerID = setTimeout(stopTrying, options.maxWait); // Set a timeout that will abandon the location loop
};
然后我打电话给:
navigator.geolocation.getAccurateCurrentPosition(onSuccess, onError, onProgress, {desiredAccuracy:20, maxWait:15000});
当它找到一个位置时,它会触发 geolocationSuccess(position);
(上述功能成功。问题是在我的 PhoneGap 应用程序中,它似乎经常多次调用它。有时 4-5同一秒内的次数。
为什么会这样?我只初始化它一次,它不在任何循环中。
脚本作者在 github readme 上回答了您的问题(重点是我的):
A better way to do this is to use navigator.geolocation.watchPosition(). This method will do a callback every time the location changes or every time the device improves the accuracy (based on my observations). In my own testing with a freshly booted device, it will take between 2 and 6 callbacks to get to something highly accurate. This led me to write this very simple JavaScript function that uses watchPosition() in combination with a simple timer.
所以作者在这个实现中使用的是geolocation watchPosition()
方法。
如果位置或精度发生变化,watchPosition()
方法的回调可能会被调用多次。参见documentation(重点又是我的):
If the position data changes (either by device movement or if more accurate geo information arrives), you can set up a callback function that is called with that updated position information. This is done using the watchPosition() function, which has the same input parameters as getCurrentPosition(). The callback function is called multiple times, allowing the browser to either update your location as you move, or provide a more accurate location as different techniques are used to geolocate you. The error callback function, which is optional just as it is for getCurrentPosition(), can be called repeatedly.