android上的javascript geolocation在离线模式下不起作用

时间:2012-12-04 12:37:16

标签: android cordova geolocation

您好我想知道是否可以在没有互联网连接的情况下捕获地理位置。说我没有网络,但我有我的Gps,确实

navigator.geolocation.getCurrentPosition(successCallback, errorCallback,
  {
    enableHighAccuracy : true,
    timeout : 10000, // 10s
    maximumAge : 0
  } 

在这种情况下工作

2 个答案:

答案 0 :(得分:1)

当我开发我的phonegap应用程序(在android上)时,我发现使用enableHighAccuracy并不总是返回准确的位置,有时操作系统将返回精度为100的精度位置,我假设使用网络三角测量或互联网连接以确定位置。

此外,如果您在应用程序运行时丢失GPS定位,操作系统有时会返回不准确的位置。 AFAIK无法判断返回到successCallback()的位置是哪种方法,但我通过检查返回到successCallback()的响应对象中的准确度值来推断这一点:我设置了50米的最低要求精度;如果返回的位置不如此准确,我会忽略它们。

试用此测试用例,看看您的设备返回的准确度。由于navigator.geolocation.getCurrentPosition()只返回你的位置一次,我使用的是navigator.geolocation.watchPosition()。希望这有帮助!

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Geolocation test</title>

<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src ="jquery-1.7.1.min.js"></script>

<script type="text/javascript" >    
    $(document).on("deviceready", function() {
        var minimumAccuracy = 50; // Minimum accuracy in metres to accept as a reliable position

        function successCallback(position){
            $('#error').html('');
            $('#lat').html(position.coords.latitude);
            $('#lon').html(position.coords.longitude);
            $('#accuracy').html(position.coords.accuracy);
            $('#accurate').html(position.coords.accuracy > minimumAccuracy ? "true" : "false");

        }

        function errorCallback(error){
            $('#lat').html(''); $('#lon').html(''); $('#accuracy').html(''); $('#accurate').html('');
            $('#error').html("Error while retrieving current position. <br/>Error code: " + error.code + "<br/>Message: " + error.message);
        }

        navigator.geolocation.watchPosition(successCallback, errorCallback, {
            enableHighAccuracy: true,
            timeout: 10000,
            maximumAge: 0

        });
    });

</script>
</head>
<body>
<p>Latitude: <span id="lat"></span></p>
<p>Longitude: <span id="lon"></span></p>
<p>Accuracy: <span id="accuracy"></span></p>
<p>Accurate?: <span id="accurate"></span></p>
<hr/>
<p>Error: <span id="error"></span></p>
</body>
</html>

答案 1 :(得分:1)

对于Android应用程序,您可以使用http://www.offlinegeolocation.com中的可下载Android库 - 它是免费的,但相当大(全球240 MB,仅限英国10 Mb)。它使用已知的蜂窝塔位置,并将其与检测到的附近蜂窝塔进行比较。它不如GPS或在线地理位置准确,但可能适合您的故障转移需求。

为了在Phonegap中使用它,需要将其转换为Phonegap插件。这里有一个很好的教程:http://devgirl.org/2013/07/17/tutorial-how-to-write-a-phonegap-plugin-for-android/

相关问题