Android 在clearwatch之后,watchPosition不重置高精度
我在一个小应用程序上使用watchPosition,我同时使用高精度定位(GPS)和粗略定位(基于网络)来限制电池消耗 要启用粗略定位,我使用以下命令:Android 在clearwatch之后,watchPosition不重置高精度,android,cordova,gps,Android,Cordova,Gps,我在一个小应用程序上使用watchPosition,我同时使用高精度定位(GPS)和粗略定位(基于网络)来限制电池消耗 要启用粗略定位,我使用以下命令: watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: false });` 为了实现精确定位: watchID = navigator.geolocation.watchPosition(onSuccess, onErro
watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: false });`
为了实现精确定位:
watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: false });
要重置监视,请使用clearWatch:
navigator.geolocation.clearWatch(watchID);
奇怪的是,在启用精细定位后,我无法切换回粗略定位(android上的GPS图标仍然打开)
例如:
watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: false }); // GPS icon is off which is OK
navigator.geolocation.clearWatch(watchID); //GPS icon still off (OK)
watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true }); // GPS icon become on (which is OK)
navigator.geolocation.clearWatch(watchID); //GPS icon become off (OK)
watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: false }); // GPS icon is on which is **NOT OK**
我使用的是安卓5.1.1
完整示例代码:
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", WatchCoarse, false);
var watchID = null;
// device APIs are available
//
function WatchCoarse() {
// Get the most accurate position updates available on the
// device.
var options = { enableHighAccuracy: false };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: false });
}
function WatchFine() {
// Get the most accurate position updates available on the
// device.
var options = { enableHighAccuracy: true };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}
// onSuccess Geolocation
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'<hr />' + element.innerHTML;
}
// clear the watch that was started earlier
//
function clearWatch() {
if (watchID != null) {
navigator.geolocation.clearWatch(watchID);
watchID = null;
}
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
</script>
</head>
<body>
<p id="geolocation">Watching geolocation...</p>
<button onclick="clearWatch();">Clear Watch</button>
<button onclick="WatchCoarse();">Watch Coarse</button>
<button onclick="WatchFine()">Watch Fine</button>
</body>
</html>
设备属性示例
//等待加载设备API库
//
文件。添加的监听器(“DeviceRady”,WatchRough,false);
var-watchID=null;
//设备API可用
//
函数watchrough(){
//在上获取最准确的位置更新
//装置。
var options={enableHighAccurance:false};
watchID=navigator.geolocation.watchPosition(onSuccess,onError,{enableHighAccurance:false});
}
函数WatchFine(){
//在上获取最准确的位置更新
//装置。
var options={enableHighAccurance:true};
watchID=navigator.geolocation.watchPosition(onSuccess、onError、options);
}
//成功地理定位
//
成功时的功能(位置){
var element=document.getElementById('geolocation');
element.innerHTML='Latitude:'+position.coords.Latitude+'
'+
'经度:'+position.coords.Longitude+'
'+
“
”+element.innerHTML;
}
//清除先前启动的手表
//
函数clearWatch(){
if(watchID!=null){
navigator.geolocation.clearWatch(watchID);
watchID=null;
}
}
//OneError回调接收PositionError对象
//
函数onError(错误){
警报('code:'+error.code+'\n'+
'消息:'+error.message+'\n');
}
查看地理定位
清表
粗看
小心