Phonegap / Cordova地理位置不适用于Android [英] Phonegap/Cordova geolocation not working on Android
问题描述
我有一个麻烦,使地理位置工作在Android上的模拟器(即使当我地理定位
通过telnet)和在设备上。
I'm having a trouble to get Geolocation working on Android in both emulator (even when I geo fix
over telnet) and on device. Works on iOS, WP8 and in the browser.
当我使用下面的代码向设备请求位置时,我总是得到一个错误(在我的情况下自定义检索您的位置失败,原因不明。与
null
错误代码和错误消息)。
When I ask device for location using the following code, I always get an error (in my case custom Retrieving your position failed for unknown reason.
with null
both error code and error message).
相关代码:
successHandler = (position) ->
resolve App.Location.create
lat: position.coords.latitude
lng: position.coords.longitude
errorHandler = (error) ->
error = switch error.code
when 1
App.LocationError.create
message: 'You haven\'t shared your location.'
when 2
App.LocationError.create
message: 'Couldn\'t detect your current location.'
when 3
App.LocationError.create
message: 'Retrieving your position timeouted.'
else
App.LocationError.create
message: 'Retrieving your position failed for unknown reason. Error code: ' + error.code + '. Error message: ' + error.message
reject(error)
options =
maximumAge: Infinity # I also tried with 0
timeout: 60000
enableHighAccuracy: true
navigator.geolocation.getCurrentPosition(successHandler, errorHandler, options)
platforms / android / AndroidManifest.xml
platforms/android/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" />
www / config.xml(以防万一)
www/config.xml (just in case)
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>
使用Cordova 3.1.0。在Android 4.2上测试。安装插件。包含在index.html中的Cordova.js(其他插件,如InAppBrowser工作正常)。
Using Cordova 3.1.0. Testing on Android 4.2. Plugin installed. Cordova.js included in index.html (other plugins like InAppBrowser are working fine).
$ cordova plugins ls
[ 'org.apache.cordova.console',
'org.apache.cordova.device',
'org.apache.cordova.dialogs',
'org.apache.cordova.geolocation',
'org.apache.cordova.inappbrowser',
'org.apache.cordova.vibration' ]
我很无能。
推荐答案
尝试这种方式,看看它是否适合你,因为它对我有用:
Try options this way to see if it works for you as it has for me:
var options = { enableHighAccuracy: true };
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
这篇关于Phonegap / Cordova地理位置不适用于Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!