phonegap获取地理位置错误“User denied Geolocation”


大致代码是这样的:


 navigator.geolocation.getCurrentPosition(
    //onSuccess
    function(position) {
        //百度LBS云检索
        joFile(
            'http://api.map.baidu.com/geosearch/poi?location=' + position.coords.latitude + ',' + position.coords.longitude + '&filter=databox:xxx&scope=2&ak=' + TOG.BaiduAppKey,
            function(data, error) {
                if(error) {
                    //display the error
                    return;
                } else {
                    result = JSON.parse(data);
                    for(var i = 0; i < result.size; i++) {
                        //handle the data returned
                    }
                }
            }
        );
    },
    //onError
    function(error) {
        console.dir(error);
        alert(error.message);
    },
    //options
    { enableHighAccuracy: true }
);

在chrome上调试的时候弹出“User Denied Geolocation”,将chrome设置为允许任何网站获取位置,错误依旧出现。stackoverflow上说是因为chrome的安全策略,会组织file:///开头的网页获取用户地理位置。
于是我在android emulator中测试,错误依旧出现。
于是考虑是不是去权限问题,按照官方文档中的做法又重新设置了一遍权限(虽然我明明已经设置好了):

app/res/xml/config.xml

<plugin name="Geolocation" value="org.apache.cordova.GeoBroker" />

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" />

悲催的是错误依旧出现。
stackoverflow上又说是www目录下缺少config.xml,复制过去,错误依旧出现。
stackoverflow上又说是ddms没设置gps,于是我直接打包成apk真机调试。
错误依旧出现。

我觉得这应该是有解决方法的。虽然phonegap文档中说:

This API is based on the W3C Geolocation API Specification. Some devices (Android, BlackBerry, Bada, Windows Phone 7, webOS and Tizen, to be specific) already provide an implementation of this spec. For those devices, the built-in support is used instead of replacing it with Cordova's implementation. For devices that don't have geolocation support, the Cordova implementation adheres to the W3C specification.

这。。。

chrome Android phonegap html5

青春划破流沙 10 years, 10 months ago

如果你去看phonegap的源码你会发现 { enableHighAccuracy: true } 这个参数有特殊处理,表示是否获得更高的精确度。

设置为true,在内部会调用GPS模块,也就会是GPS定位。你GPS没开启呢,所以报错了。如果不想开启GPS,把这个设置为false吧。。

其实设置为false表示通过手机网络定位,为true是GPS定位。

edora answered 10 years, 10 months ago

Your Answer