javascript - Geolocation with PhoneGap on Android -
i work on application using phonegap. moment, test on android. have several pages in application need geolocation feature.
so made js handle line of code (of course it's not unique line of code) : navigator.geolocation.watchposition(successgeolocation, errorgeolocation, {maximumage: 5000, enablehighaccuracy: true});
the geolocation need accurate possible use gps , can take time have gps fix.
the problem when user navigates 1 page another. watchposition stop (it's normal because user load other page) , when recall gps need fix again.
it's annoying , search solution keep gps active. has idea me ? maybe plugin or native android loc can keep active during application life ?
thanks.
first step should create android plugin give api receiving location data.
creating plugin quite easy , explained here: plugin development guide , developing plugin on android. can see example of creating cordova plugin here.
next, create location monitor class. can make singleton , initialize main activity.
here simple, working code compiled several sources , many tests fit needs. main code taken here, though simplified as possible.
public class locationmonitor { private locationlistener locationlistener = null; private locationmanager locationmanager = null; private location location = null; public locationmonitor() { } public void startgpsactivity(context context) { locationlooper looper = new locationlooper(); looper.start(); while (!looper.isready()) { } looper.handler.post(new locationbootstrapper(context)); } public void stopgpsactivity() { locationmanager.removeupdates(locationlistener); } public location getlocation() { return location; } private class locationlooper extends thread { private handler handler; private locationlooper() { } public void run() { looper.prepare(); this.handler = new handler(); looper.loop(); } public boolean isready() { return this.handler != null; } } private class locationbootstrapper implements runnable { private context context; private locationbootstrapper(context context) { this.context = context; } public void run() { locationlistener = new locationlistenerimpl(); locationmanager = (locationmanager)context.getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 1, locationlistener); } } private class locationlistenerimpl implements locationlistener { private locationlistenerimpl() { } @override public void onlocationchanged(location location) { locationmonitor.this.location = location; log.i("locationmonitor", "new location: lat= " + location.getlatitude() + " lng=" + location.getlongitude() + " acc=" + location.getaccuracy()); } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } } }
now access locationmonitor class plugin , have desired solution - page changes not re-initialize gps , location data available phonegap app.
cheers
Comments
Post a Comment