android - Making gps calls faster and more efficient -
i've been working on simple speed calculations application i'm working on, code have takes far long retrieve location, , know questions have been asked before, none of answers seem retrieve results i'm looking for. so, how can make code have gps fixes within matter of seconds, , possible? locationlistener:
package me.dylan.acf; import java.text.decimalformat; import java.util.arraylist; import android.app.notificationmanager; import android.location.location; import android.location.locationlistener; import android.os.bundle; import android.text.format.time; import android.widget.textview; public class gpsmanager implements locationlistener { arraylist<double> avgspeeds = new arraylist<double>(); textview debug; notificationmanager mngr; double avgspeed; long lasttime = 0; graphview view; location lastloc; int earthradius = 6371; long delaytime = 30; arraylist<double> allspeeds = new arraylist<double>(); public gpsmanager(textview view) { debug = view; location location = acf.instance.lmanager .getlastknownlocation(acf.instance .getproperlocationsservices(acf.instance .getapplicationcontext())); if (location != null) { double speed = location.getspeed(); lastloc = location; debug.settext("average speed: " + avgspeed + "\ncurrent speed: " + speed + "\nlocation updates: " + avgspeeds.size()); } } @override public void onlocationchanged(location location) { // decimalformat format = new decimalformat("0.00"); double speed = location.getspeed(); if (lastloc != null) { double latdist = math.toradians(location.getlatitude() - lastloc.getlatitude()); double londist = math.toradians(location.getlongitude() - lastloc.getlongitude()); double lat1 = math.toradians(location.getlatitude()); double lat2 = math.toradians(lastloc.getlatitude()); double = math.sin(latdist / 2) * math.sin(latdist / 2) + math.sin(londist / 2) * math.sin(londist / 2) * math.cos(lat1) * math.cos(lat2); double c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)); double dist = earthradius * c; speed = (dist * 0.621371) / math.abs(system.currenttimemillis() - lasttime * 60 * 60 * 60); lasttime = system.currenttimemillis(); } allspeeds.add(speed); if (allspeeds.size() > 30) { allspeeds.remove(0); } avgspeed = 0; (double d : allspeeds) { avgspeed += d; } avgspeed /= allspeeds.size(); // avgspeed = double.parsedouble(format.format(avgspeed)); avgspeeds.add(avgspeed); lastloc = location; debug.settext("average speed: " + avgspeed + "\ncurrent speed: " + speed + "\nlocation updates: " + avgspeeds.size()); } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } }
where call it:
public void updatewithproperservice() { lmanager.requestsingleupdate( getproperlocationsservices(getapplicationcontext()), gpsmngr, null); timer timer = new timer(); timer.schedule(new timertask() { @override public void run() { updatewithproperservice(); } }, 10000); } public string getproperlocationsservices(context context) { if (lmanager == null) lmanager = (locationmanager) context .getsystemservice(context.location_service); int mintime = 3000; /* * boolean isgps = false; boolean isnetwork = false; try { isgps = * lmanager.isproviderenabled(locationmanager.gps_provider); } catch * (exception e) { e.printstacktrace(); } try { isnetwork = lmanager * .isproviderenabled(locationmanager.network_provider); } catch * (exception e) { e.printstacktrace(); } */ list<string> matchingproviders = lmanager.getallproviders(); location bestresult = null; long besttime = 0; (string provider : matchingproviders) { location location = lmanager.getlastknownlocation(provider); if (location != null) { // float accuracy = location.getaccuracy(); long time = location.gettime(); // float bestaccuracy; /* * if ((time > mintime && accuracy < bestaccuracy )) { * bestresult = location; besttime = time; } else */if (time < mintime && /* bestaccuracy == float.max_value && */time < besttime) { bestresult = location; besttime = time; } } } if (bestresult != null) return bestresult.getprovider(); else return locationmanager.network_provider; }
google has released nice api on recent google io 2013 event:
https://developers.google.com/events/io/sessions/324498944
you should check out , see how can minimize code.
do note requires device have play store app work.
this method has many advantages on using normal location sensors (battery, speed , accuracy,...) .
Comments
Post a Comment