The following code
navigator.geolocation.getCurrentPosition(getGeo_Success, getGeo_Fail, {
enableHighAccuracy : true,
maximumAge : Infinity,
timeout : 15000
});
Retrieves the current GPS position – BUT – there must be a valid GPS signal (and ofcourse, the device’s GPS feature should be turned on).
If we look at other applications (say, Maps on Android devices) – it knows how to retrieve the last known position – even if I didn’t use an application that updates the geolocation before opening Maps – it shows my position on the map, even if I’m inside a building with no GPS signal at all.
Just to clarify: I’m not interested in the last geolocation my application retrieved, as on the next time I`ll start it, that geolocation will probably be irrelevant.
Question is: How can we achieve this with HTML5/Phonegap? seems like navigator.geolocation only knows to retrieve the current position, even though, maximumAge is set to Infinity (which means, the age of the last cached position is irrelevant, so any hit is okay (or, should be!))
ANDROID SOLUTION (iPhone solution follows):
This is neat:
I’ve made use of Android’s native
LocationManager, which provides agetLastKnownLocationfunction – the name says it allHere’s the relevant code
1) Add the following java class to your application
2) Make sure your main class looks as follows:
3) on your JS code, just call the native java using:
Thats all folks! 🙂
EDIT:
iPhone solution:
I wrote a tiny Phonegap plugin that creates an interface to a custom class which is making use of iOS’s native
CLLocationManager1) Phonegap plugin (JS)
2) The objective-c class that enables us to invoke ‘CCLocationManager’s functions
*NativeLocation.h*
NativeLocation.m
3) And finally, invoking everything from the main .js