我有一个看似非常直截了当的问题,但我无法理解我哪里出错!
我正在使用PhoneGap(2.9版本)来获取地理位置数据。在初始化时调用以下函数(getGeolocationData)。问题在于位置服务条件......
如果在应用程序启动时启用了位置服务,则地理定位 获得数据。 (OK)
如果应用程序启动时位置服务已关闭,则会出现错误 显示并再次调用地理定位函数。 (OK)
如果在应用程序启动时启用了位置服务,则会获得地理位置数据,但在使用期间如果禁用位置服务,则不会出现错误! (ERROR)。
注意:当关闭位置服务时,不再调用警告消息“geoGeo called”。
任何人都可以帮我解决这个烦人的问题吗?
提前致谢!
瑞安function getGeolocationData() {
alert("getGeo called.");
var options = { maximumAge: 7000, timeout: 7000, enableHighAccuracy: true };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}
// onSuccess Geolocation
function onSuccess(position) {
lng = position.coords.longitude;
lat = position.coords.latitude;
acc = position.coords.accuracy;
alt = position.coords.altitude;
hdg = position.coords.heading;
spd = position.coords.speed;
geo_flag = 1; //obtained geolocation? 1 = Yes / 0 = No
document.getElementById("geo-status").innerHTML = 'On';
alert("success");
}
// onError Callback receives a PositionError object
function onError(error) {
document.getElementById("tracking-status").innerHTML = 'Off';
document.getElementById("geo-status").innerHTML = 'Off';
geo_flag = 0; //obtained geolocation? 1 = Yes / 0 = No
alert("failed");
getGeolocationData();
}
答案 0 :(得分:0)
我认为你是在iOS设备上测试它,因为打开/关闭“位置服务”?
似乎Phonegap仅在应用启动时添加观察程序时检查位置服务的可用性,而不是每次更新时都连续添加。由于您需要将应用程序置于后台以访问“设置”以禁用位置设置,因此您可以尝试使用Phonegap resume事件来清除/重新添加观察程序,并查看是否会导致Phonegap注意到位置服务的状态已经改变。像这样:
var watchID;
function getGeolocationData() {
alert("getGeo called.");
var options = { maximumAge: 7000, timeout: 7000, enableHighAccuracy: true };
if(watchID) navigator.geolocation.clearWatch(watchID);
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}
// onSuccess Geolocation
function onSuccess(position) {
lng = position.coords.longitude;
lat = position.coords.latitude;
acc = position.coords.accuracy;
alt = position.coords.altitude;
hdg = position.coords.heading;
spd = position.coords.speed;
geo_flag = 1; //obtained geolocation? 1 = Yes / 0 = No
document.getElementById("geo-status").innerHTML = 'On';
alert("success");
}
// onError Callback receives a PositionError object
function onError(error) {
document.getElementById("tracking-status").innerHTML = 'Off';
document.getElementById("geo-status").innerHTML = 'Off';
geo_flag = 0; //obtained geolocation? 1 = Yes / 0 = No
alert("failed");
getGeolocationData();
}
// Called on device being ready
function onDeviceReady(){
getGeolocationData(); // start geolocation tracking
}
// Called on resuming app from background
function onResume(){
alert("app resumed");
getGeolocationData(); // reset geolocation tracking
}
document.addEventListener("deviceready", onDeviceReady, false);
document.addEventListener("resume", onResume, false);