Geolocation正在给timeOut错误cordova android应用程序

时间:2015-03-27 16:26:54

标签: geolocation phonegap-plugins cordova-plugins

我正在使用visual studio Cordova工具来创建应用程序。我正在使用HTML5的地理位置来获取用户位置。

当我在ripple naxus-galaxy中执行它时,它运行正常,但是当我在Android设备中运行它时,它根本不工作。它显示每次都有TimeOut错误。

我已经安装了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文件中更改某些内容??

1 个答案:

答案 0 :(得分:2)

getCurrentPosition()针对设备位置发出单一请求。如果在应用程序启动后立即调用此项,则可能会在设备上的GPS硬件有机会获得定位之前发生超时。

我建议使用watchPosition()来设置一个观察程序,每次操作系统收到位置更新时都会调用成功函数。如果您只需要一个位置,那么一旦您获得了可接受的准确度,您就可以清除观察者。

例如:

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);