java - Google Play services location API - Counting distance -
i've been following https://developer.android.com/training/location/index.html tutorial.
here's class implementing new google's location api:
public class gpstracker implements locationlistener, googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener { private final context context; private textview distancetextview; private googleapiclient mgoogleapiclient; private locationrequest mlocationrequest; private location mlastlocation; private double totalmeters = 0; public gpstracker(context context, textview distancetextview) { this.context = context; this.distancetextview = distancetextview; buildgoogleapiclient(); } protected synchronized void buildgoogleapiclient() { mgoogleapiclient = new googleapiclient.builder(context) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); mgoogleapiclient.connect(); } @override public void onlocationchanged(location location) { double distance = mlastlocation.distanceto(location); mlastlocation = location; totalmeters += distance; distancetextview.settext(totalmeters + ""); } @override public void onconnected(bundle bundle) { mlocationrequest = locationrequest.create(); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); mlocationrequest.setinterval(1000); // update location every second locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); mlastlocation = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); } @override public void onconnectionsuspended(int i) { } @override public void onconnectionfailed(connectionresult connectionresult) { toast.maketext(context, connectionresult.geterrormessage(), toast.length_short).show(); } }
everything seems working flawlessly, when phone not moving @ totaldistance still rising 0.2-0.3 meters every time onlocationchanged method executed.
any solution prevent situation highly appreciated.
don't forget gps not pin point accurate. there off few meters it's natural there incorrect readings this. while old locationmanager had mindistance settings, locationrequest unfortunately not.
several solutions: can check speed, if speed zero, indicates user hasn't moved. can manually check if old location same new 1 (in case 0.3m seems threshold) , discard reading. last not least can set activityrecognitionapi activity listener determine if user moving or still.
Comments
Post a Comment