Tuesday, 15 March 2011

gps - Location.distanceTo not giving correct results in android -



gps - Location.distanceTo not giving correct results in android -

i trying calculate distance between 2 locations (current location previous location).

so tried following:

location previouslocation = new location(""); previouslocation.setlatitude(sharedpreferences.getfloat("previouslatitude", 0f)); previouslocation.setlongitude(sharedpreferences.getfloat("previouslongitude", 0f)); float distance = location.distanceto(previouslocation); totaldistanceinmeters += distance; editor.putfloat("totaldistanceinmeters", totaldistanceinmeters); log.e("location update","totaldistance"+totaldistanceinmeters); if (totaldistanceinmeters > 1) { toast.maketext(getapplicationcontext(), "total updatelocation"+totaldistanceinmeters/1609, toast.length_short).show(); log.e("alert","update"); }

to test above code. first time result perfect , when triggered sec time. phone in same location getting distances 141.0111 m. thrid time 304.0011 m. doing wrong here?

the results not showing correctly. according doc online results in metres.

is there easy way calculate difference between first location results sec 1 , if more 10m other calculation if not maintain quite.

let me know.

why using next code

float distance = location.distanceto(previouslocation); totaldistanceinmeters += distance;

that adds previous distance nowadays distance , gives added value everytime....

example

first time distance between , b 100 m sec time distance between , b 100 m+100 m=200 m

so seek using distance straight in toast

float distance = location.distanceto(previouslocation); if (totaldistanceinmeters > 1) { toast.maketext(getapplicationcontext(), "total updatelocation"+distance/1609,toast.length_short).show(); log.e("alert","update"); }

android gps location

No comments:

Post a Comment