PhoneGap地理定位器使用WiFi而不是GPS

PhoneGap geolocator using WiFi instead of GPS

本文关键字:GPS WiFi 定位器 PhoneGap      更新时间:2023-09-26

我正在尝试在Android上运行的PhoneGap应用程序上实现地理定位。出于某种原因,定位器仅在连接WiFi时返回准确的距离(即它更喜欢粗略而不是精细(。我的GPS当然已启用。请注意,该应用程序曾一度尝试使用 GPS(我看到定位器闪光灯出现(,但它一直超时;重新启动设备后,它又恢复为仅使用WiFi。附上我的代码:

function testGeo(){
        navigator.geolocation.getCurrentPosition(function(position){
            $('#latitude').html('');
            $('#longitude').html('');
            $('#accuracy').html('');
            $('#latitude').html(position.coords.latitude);
            $('#longitude').html(position.coords.longitude);
            $('#accuracy').html(position.coords.accuracy);
            $('#loading-frame-geo').hide();
        }, GeoError, {enableHighAccuracy:true,maximumAge:3000,timeout:10000});  
}

我正在使用PhoneGap版本2.5.0(用于与其他功能的向后兼容性(

确保在

配置中启用了地理位置.xml:<plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/>

在你的 AndroidManifest 中.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

还可以尝试将超时增加到 60000 - 旧设备可能需要一段时间才能获得 GPS 定位。

考虑使用 navigator.geolocation.watchPosition 而不是 navigator.geolocation.getCurrentPosition 。这将添加一个观察程序,该观察程序在每次设备获得新位置时调用成功函数;如果尚未获取仓位修复,将调用错误回调。

还有一件事(我从经验中发现(是,如果Android丢失了GPS定位,并且传递给成功函数的Position对象中没有任何内容可以告诉您设备正在使用什么硬件来获取其位置,那么它将默默地回退到Wifi或蜂窝三角测量。但是,您可以从准确性推断出它并丢弃任何过于不准确的位置,例如:

MIN_ACCURACY = 20; //metres
function success(position){
  if(position.coords.accuracy > MIN_ACCURACY){
    console.log("Position rejected because accuracy is less than required "+MIN_ACCURACY+" metres");
    return;
  }
  // else, do some stuff with the position
}

您可能还需要考虑使用此插件来检查设备上是否isGpsEnabled()启用了 GPS,如果没有,请将用户定向到设置页面,并带有switchToLocationSettings()

希望对你有帮助...