kavinhuh
kavinhuh

Reputation: 739

javascript geolocation on android doesn't work on offline mode

Hi I would like to know whether is it possible to capture the geo location without the internet connectivity. Say I am not connected to net but I have my Gps on, does

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

work in this scenario

Upvotes: 3

Views: 2663

Answers (2)

Fiach Reid
Fiach Reid

Reputation: 7059

For an Android application, you can use a downloadable Android Library from http://www.offlinegeolocation.com - It's free, but quite large (240 MB for Worldwide, 10 Mb for UK only). It uses known cell tower locations and compares this against detected nearby cell towers. It is not as accurate as GPS or Online Geolocation, but might suit your needs as a failover.

In order to use this with Phonegap, it would need to be converted into a Phonegap Plugin. There is a good tutorial on this here: http://devgirl.org/2013/07/17/tutorial-how-to-write-a-phonegap-plugin-for-android/

Upvotes: 1

DaveAlden
DaveAlden

Reputation: 30366

While I was developing my phonegap app (on android) I found that using enableHighAccuracy doesn’t always return accurate positions, sometimes the operating system will return a low accuracy position in the order of 100s of metres accuracy, I assume by using network triangulation or internet connection to determine location.

Also if you lose your GPS fix while your app is running, the OS sometimes return an inaccurate position. AFAIK there’s no way to tell by which method the position returned to successCallback() was obtained, but I inferred this by inspecting the accuracy value in the response object returned to successCallback(): I set a minimum required accuracy of 50 metres; if the returned positions are less accurate than this, I ignore them.

Try out this test case and see what accuracy your device is returning. Since navigator.geolocation.getCurrentPosition() only returns your position once, I’m using navigator.geolocation.watchPosition(). Hope this helps!

<!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>

Upvotes: 1

Related Questions