Phonegap / Cordova地理位置不适用于Android该怎么办?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (10)

我的相关代码如下:

    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)

平台/安卓/ 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:

<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等其他插件工作正常):

$ 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' ]
提问于
用户回答回答于

你可以这样删除地理位置插件:

cordova plugin rm org.apache.cordova.geolocation

还有请确保AndroidManifest.xml和config.xml属性保持不变:

(in app/res/xml/config.xml)
<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>


(in app/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" />

对于config.xml中的iOS:

<feature name="Geolocation">
    <param name="ios-package" value="CDVLocation" />
</feature>
用户回答回答于

以这种方式尝试选项,看看它是否适合你,因为它对我有用:

var options = { enableHighAccuracy: true };

navigator.geolocation.getCurrentPosition(onSuccess, onError, options);

扫码关注云+社区