地理定位给出超时错误 cordova android 应用程序
Geolocation is giving timeOut error cordova android application
我正在使用 visual studio Cordova 工具创建应用程序。我正在使用 HTML5 的地理定位来获取用户位置。
当我在 ripple naxus-galaxy 中执行它时,它工作正常,但是当我在 android 设备中 运行 它时,它根本不工作。它显示我每次都发生超时错误。
我已经安装了 geoLocation 插件。
我的代码是,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>GeoLal</title>
<!-- GeoLal references -->
<link href="css/index.css" rel="stylesheet" />
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
</head>
<body>
<p>Hello, your application is ready!</p>
<div id="map-canvas" style="position:absolute; width:100%; height:100%"></div>
<script>
var map;
function initialize() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError, { timeout: 15000, enableHighAccuracy: true });
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
alert("Pos = " + JSON.stringify(position));
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("Please share your location with us to move ahead.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is not available, Check your internet connection.");
break;
case error.TIMEOUT:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("We are not able to fetch your location details.");
break;
}
initialize();
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<!-- Cordova reference, this is added to your app when it's built. -->
<script src="cordova.js"></script>
<script src="scripts/platformOverrides.js"></script>
<script src="scripts/index.js"></script>
</body>
</html>
而web.config是,
<?xml version="1.0" encoding="utf-8"?>
<widget xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps" id="io.cordova.GeoLal" version="1.0.0.0" xmlns="http://www.w3.org/ns/widgets">
<name>GeoLal</name>
<description>A blank project that uses Apache Cordova to help you build an app that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.</description>
<author href="http://cordova.io" email="dev@cordova.apache.org">Apache Cordova Team </author>
<content src="index.html" />
<access origin="*" />
<preference name="SplashScreen" value="screen" />
<preference name="windows-target-version" value="8.0" />
<preference name="windows-phone-target-version" value="8.1" />
<vs:plugin name="org.apache.cordova.geolocation" version="0.3.10" />
<vs:platformSpecificValues />
</widget>
我需要更改 web.config 文件中的某些内容吗...?
getCurrentPosition()
发出单个设备位置请求。如果在应用程序启动后立即调用此方法,则可能会在设备上的 GPS 硬件有机会获得定位之前发生超时。
我建议使用 watchPosition()
来设置一个观察器,它会在每次 OS 收到位置更新时调用成功函数。如果您只需要一个位置,则可以在获得可接受精度的位置后清除观察器。
例如:
var minAccuracyInMetres = 50;
var positionWatcher;
function onDeviceReady(){
positionWatcher = navigator.geolocation.watchPosition(
geolocationSuccess,
geolocationError,
{maximumAge:0, timeout: 15000, enableHighAccuracy: true});
}
function geolocationSuccess(position){
// Reject if accuracy is not sufficient
if(position.coords.accuracy > minAccuracyInMetres){
return;
}
// Only single position required so clear watcher
navigator.geolocation.clearWatch(positionWatcher);
// Do something with the user's location...
}
document.addEventListener("deviceready", onDeviceReady, false);
我正在使用 visual studio Cordova 工具创建应用程序。我正在使用 HTML5 的地理定位来获取用户位置。
当我在 ripple naxus-galaxy 中执行它时,它工作正常,但是当我在 android 设备中 运行 它时,它根本不工作。它显示我每次都发生超时错误。
我已经安装了 geoLocation 插件。
我的代码是,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>GeoLal</title>
<!-- GeoLal references -->
<link href="css/index.css" rel="stylesheet" />
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
</head>
<body>
<p>Hello, your application is ready!</p>
<div id="map-canvas" style="position:absolute; width:100%; height:100%"></div>
<script>
var map;
function initialize() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError, { timeout: 15000, enableHighAccuracy: true });
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
alert("Pos = " + JSON.stringify(position));
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("Please share your location with us to move ahead.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is not available, Check your internet connection.");
break;
case error.TIMEOUT:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("We are not able to fetch your location details.");
break;
}
initialize();
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<!-- Cordova reference, this is added to your app when it's built. -->
<script src="cordova.js"></script>
<script src="scripts/platformOverrides.js"></script>
<script src="scripts/index.js"></script>
</body>
</html>
而web.config是,
<?xml version="1.0" encoding="utf-8"?>
<widget xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps" id="io.cordova.GeoLal" version="1.0.0.0" xmlns="http://www.w3.org/ns/widgets">
<name>GeoLal</name>
<description>A blank project that uses Apache Cordova to help you build an app that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.</description>
<author href="http://cordova.io" email="dev@cordova.apache.org">Apache Cordova Team </author>
<content src="index.html" />
<access origin="*" />
<preference name="SplashScreen" value="screen" />
<preference name="windows-target-version" value="8.0" />
<preference name="windows-phone-target-version" value="8.1" />
<vs:plugin name="org.apache.cordova.geolocation" version="0.3.10" />
<vs:platformSpecificValues />
</widget>
我需要更改 web.config 文件中的某些内容吗...?
getCurrentPosition()
发出单个设备位置请求。如果在应用程序启动后立即调用此方法,则可能会在设备上的 GPS 硬件有机会获得定位之前发生超时。
我建议使用 watchPosition()
来设置一个观察器,它会在每次 OS 收到位置更新时调用成功函数。如果您只需要一个位置,则可以在获得可接受精度的位置后清除观察器。
例如:
var minAccuracyInMetres = 50;
var positionWatcher;
function onDeviceReady(){
positionWatcher = navigator.geolocation.watchPosition(
geolocationSuccess,
geolocationError,
{maximumAge:0, timeout: 15000, enableHighAccuracy: true});
}
function geolocationSuccess(position){
// Reject if accuracy is not sufficient
if(position.coords.accuracy > minAccuracyInMetres){
return;
}
// Only single position required so clear watcher
navigator.geolocation.clearWatch(positionWatcher);
// Do something with the user's location...
}
document.addEventListener("deviceready", onDeviceReady, false);