how capturing geolocation with device offline - cordova

14.4k views Asked by At

I am developing an application using the cordova and found a need to take the data of latitude and longitude of the User, but my application will be used offline (without internet access).

I'm with two questions:

1 - How to check if the unit is active GPS or not?

2 - How I can get the latitude and longitude Offline without internet connection? It's possible?

3

There are 3 answers

2
Alejandro Teixeira Muñoz On BEST ANSWER

I´ll try to answer you:

1.- There you have the way to use PhoneGap GeoLication API to check location, and if GPS is enabled. Check this too: Use PhoneGap to Check if GPS is enabled

2.- Yes, the GPS is a satellite system that allows your device to track it´s current position on earth. It´s independent from your Cellphone operator, and then, from the Internet Connection. Then, your device will be able to know it´s position. HTML5 offline mode and geolocation

In addition, you´ll probably need Internet connection to show a map. There are not much solutions for offline maps. Just artisan ones: http://davidrs.com/wp/phonegap-3-0-leaflet-offline-maps/

Hope it helps

REMEMBER: When turning on plane mode, most of devices will turn off GPS antenna too.

7
DaveAlden On

To check if the GPS is available or not you can use this diagnostic plugin - it works for Android and iOS. It can also be used to display the native location settings screen to allow the user to enable location services. For example:

cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
    console.log("GPS is "+(enabled ? "enabled" : "disabled");
    if(!enabled){
        cordova.plugins.diagnostic.switchToLocationSettings();
    }
},function(error){
    console.error("An error occurred: "+ error);
});

As the other answer already states, you can receive a user's location using GPS without needing an internet connection using the geolocation API, but if you want to display a map you'll need a custom solution that doesn't rely on online maps such as Google.

If you want to record the user's location while offline, you can store this information offline in local persistent storage and upload it when the user has an internet connection again. You could use something like lawnchair for the storage.

0
forstef On

You can use geolocationOptions to controll if it use the hardware gps module:

navigator.geolocation.getCurrentPosition(onSuccess, onError, { enableHighAccuracy: true });

then when your phone is gps ready, you can get gps info without network access.

enableHighAccuracy: Provides a hint that the application needs the best possible results. By default, the device attempts to retrieve a Position using network-based methods. Setting this property to true tells the framework to use more accurate methods, such as satellite positioning. (Boolean)