当我尝试在平台 Android 中使用 Cordova 获取地理定位时出现错误代码 3 超时
Error code 3 Timeout when i try to get geolocation using Cordova in platform Android
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 设备中 运行 这个相同的应用程序时,它会给我准确的 geolocation.But。它总是给超时 expired.i dnt 在控制台中有任何错误。
I had tested the my application is requested the GPS service of
android .I had checked that in location menu which have my application
in GPS request.
基于myriads of similar questions on Stack Overflow, it seems the Cordova geolocation plugin has issues especially on Android。看来解决办法是:
- 删除地理定位插件如下:
cordova plugin rm org.apache.cordova.geolocation
。
您的应用程序将依赖于标准 HTML5 navigator.geolocation
功能。
当然,请仔细检查您的定位服务是否开启,您的设备是否有信号覆盖(不是在厚墙的建筑物中等。如果是,可以将 enableHighAccuracy 设置为 false
并设置更长的超时时间)。
通过在清单中添加以下行来确保您拥有以下权限:
< 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" />
此外,为您的应用程序提供互联网访问权限,以便它可以访问 Wifi 和手机天线:
<uses-permission android:name="android.permission.INTERNET" />
我自己无法尝试这个,但你这样做后应该会有更好的运气。
告诉我你过得怎么样。
<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
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 设备中 运行 这个相同的应用程序时,它会给我准确的 geolocation.But。它总是给超时 expired.i dnt 在控制台中有任何错误。
I had tested the my application is requested the GPS service of android .I had checked that in location menu which have my application in GPS request.
基于myriads of similar questions on Stack Overflow, it seems the Cordova geolocation plugin has issues especially on Android。看来解决办法是:
- 删除地理定位插件如下:
cordova plugin rm org.apache.cordova.geolocation
。
您的应用程序将依赖于标准 HTML5 navigator.geolocation
功能。
当然,请仔细检查您的定位服务是否开启,您的设备是否有信号覆盖(不是在厚墙的建筑物中等。如果是,可以将 enableHighAccuracy 设置为
false
并设置更长的超时时间)。通过在清单中添加以下行来确保您拥有以下权限:
< 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" />
此外,为您的应用程序提供互联网访问权限,以便它可以访问 Wifi 和手机天线:
<uses-permission android:name="android.permission.INTERNET" />
我自己无法尝试这个,但你这样做后应该会有更好的运气。
告诉我你过得怎么样。
<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