使用WiFi,而不是GPS的PhoneGap的geolocator [英] PhoneGap geolocator using WiFi instead of GPS

查看:277
本文介绍了使用WiFi,而不是GPS的PhoneGap的geolocator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在Android上的PhoneGap应用程序运行的地理位置。出于某种原因,该定位器仅返回在连接无线网络(即它在粗糙prefers罚款)准确距离。当然,我的GPS已启用。请注意,在一个点上的应用程序使用GPS尝试(我看到定位信号灯拿出),但它一直超时;在重新启动设备,它回到了只使用Wi-Fi。附件是我的code:

I am trying to implement geolocation on a PhoneGap App running on Android. For some reason the locator only returns accurate distances when connected WiFi (i.e. it prefers coarse over fine). My GPS is of course enabled. Note that at one point the app tried using GPS (I saw the locator blinker come up) but it kept timing out; upon restarting the device, it went back to only using WiFi. Attached is my code:

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(与其他功能向后兼容)

I am using PhoneGap version 2.5.0 (for backwards compatibility with other features)

推荐答案

确保您有在你的config.xml中启用了地理位置:<插件名称=地理位置VALUE =org.apache。 cordova.GeoBroker/>

Ensure you have have geolocation enabled in your config.xml: <plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/>

而在你的Andr​​oidManifest.xml:

And in your 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定位

Also try upping your timeout to 60000 - it can take a while for older devices to acquire a GPS fix.

考虑使用 navigator.geolocation.watchPosition 而不是 navigator.geolocation.getCurrentPosition 。这将增加每个设备获取一个新的位置时它调用成功功能的守望者;如果位置锁定尚未取得的错误回调将被称为

Consider using navigator.geolocation.watchPosition instead of navigator.geolocation.getCurrentPosition. This will add a watcher which calls the success function each time the device acquires a new position; the error callback will be called if a position fix hasn't yet been acquired.

还有一件事(我从经验中发现)是Android将悄悄的WiFi或手机三角,如果它失去了它的GPS定位并没有什么传递到成功的功能定位对象,告诉你该设备是什么样的硬件使用来获取它的位置。但是,可以从准确推断出它与丢弃太不准确的任何职位,是这样的:

One more thing (I found from experience) is that Android will silently fall back to Wifi or cell triangulation if it loses its GPS fix and there's nothing in the Position object passed to the success function to tell you what hardware the device is using to obtain its location. However, you can infer it from the accuracy and discard any positions that are too inaccurate, something like:

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
}

您可能还需要考虑使用这个插件以检查GPS在设备上启用了 isGpsEnabled(),如果没有,直接用户的设置页面 switchToLocationSettings()

You might also want to consider using this plugin to check if GPS is enabled on the device with isGpsEnabled() and if not, direct the user to the settings page with switchToLocationSettings()

希望它可以帮助...

Hope it helps...

这篇关于使用WiFi,而不是GPS的PhoneGap的geolocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆