android - phonegap获取地理位置错误“User denied Geolocation”
黄舟
黄舟 2017-04-17 11:15:32
[Android讨论组]

大致代码是这样的:

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.ACCESSCOARSELOCATION" />
<uses-permission android:name="android.permission.ACCESSFINELOCATION" /> <uses-permission android:name="android.permission.ACCESSLOCATIONEXTRA_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.

这。。。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
PHP中文网

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

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号