【问题标题】:Error code 3 Timeout when i try to get geolocation using Cordova in platform Android当我尝试在 Android 平台中使用 Cordova 获取地理位置时出现错误代码 3 超时
【发布时间】:2016-12-23 03:46:55
【问题描述】:

index.js 代码:

// debug

function d(s) {
    console.log(s);
    $("#status").text(s);
}

// geo

function geoWin(pos) {
    d("geoWin(): "+pos.coords.latitude+", "+pos.coords.longitude+","+pos.coords.speed);
}

function geoFail(error) {
    d("geoFail(): "+error.code+": "+error.message);
}

function startGeoWatch() {
    d("startGeoWatch()");
    opt = {maximumAge: 0,timeout: 2000, enableHighAccuracy: true};
    watchGeo = navigator.geolocation.watchPosition(geoWin, geoFail, opt);
}

function stopGeoWatch() {
    d("stopGeoWatch()");
    navigator.geolocation.clearWatch(watchGeo);
}

// life cycle

function onPause() {
    d("onPause()");
    stopGeoWatch();
}

function onResume() {
    d("onResume()");
    startGeoWatch();
}

// init

function onDeviceReady() {
    d("onDeviceReady()");
    document.addEventListener("pause", onPause, false);
    document.addEventListener("resume", onResume, false);
    startGeoWatch();
}

function main() {
    document.addEventListener('deviceready', onDeviceReady, false);
}

// main & globals
var watchGeo=null;
main();

我的 config.xml 代码:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.terokarvinen.geo" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" version="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
    <plugin name="cordova-plugin-geolocation" spec="~2.2.0" />
</widget>

AndroidManifest 代码:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.terokarvinen.geo" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

当我尝试在 Firefox 中运行此应用程序时,它为我提供了确切的地理位置。但是当我尝试在 android 设备中运行相同的应用程序时。它总是给出超时过期。我在控制台中没有任何错误。

我已经测试了我的应用程序请求的 GPS 服务 android 。我已经在有我的应用程序的位置菜单中检查了它 在 GPS 请求中。

【问题讨论】:

  • 您能在 github 中分享您的项目,以便在设备中进行相同的测试吗?
  • 确定我会上传一个分享链接给你。
  • 您在 Android 上试用过哪些浏览器?
  • 我没有尝试过 o android 浏览器我已经将它构建到 android 应用程序然后在 android 设备上测试它。它在浏览器上运行良好。
  • @HassanALi 分享一下项目链接怎么样?

标签: android cordova geolocation phonegap-plugins


【解决方案1】:

基于myriads of similar questions on Stack Overflow,看来Cordova 地理定位插件有issues especially on Android。似乎解决方案是:

  1. 删除地理定位插件如下:

cordova plugin rm org.apache.cordova.geolocation

您的应用程序将使用标准 HTML5 navigator.geolocation 功能。

    1234563超时)。
  1. 通过在清单中添加这些行来确保您具有以下权限:

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

此外,为您的应用程序提供 Internet 访问权限,以便它可以访问 Wifi 和手机天线:

&lt;uses-permission android:name="android.permission.INTERNET" /&gt;

我自己无法尝试,但这样做后您应该会获得更好的运气。

让我知道你过得怎么样。

【讨论】:

  • android上的地理定位插件使用HTML5实现,只是增加了权限
  • 抱歉,我没能成功。感谢您抽出宝贵时间@ThomCunningham
【解决方案2】:
<access origin="*"/> 
<access origin="*://*.googleapis.com/*" subdomains="true" />
<access origin="*://*.gstatic.com/*" subdomains="true" />
<access origin="*://*.google.com/*" subdomains="true" />
<access origin="*://*.googleusercontent.com/*" subdomains="true" />

试试上面的代码。把它放在 config.xml 上。 检查这个Answer

【讨论】:

  • 检查请解释为什么我必须把这段代码放在我的配置中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
相关资源
最近更新 更多