Android: How to measure distance with gps -
i trying make application,which take latitude , longitude on current place of user. , count momentary,what distance travel user in km.
the "currentlat" , "currentlon" current latitude , longitude of user.but don't know latitude , longitude put "endlat" , "ednlon".
sorry bad english.
thanks in advance.
///////////////////////////////////
i made app,but have 1 little problem. when started first time program,i value 5536 , when restarted program normal value 0.0
again sorry bad english. :)
and guys helping me,you best :)
public class gps extends activity { textview display; double currentlon=0 ; double currentlat=0 ; double lastlon = 0; double lastlat = 0; double distance; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_test); display = (textview) findviewbyid(r.id.info); locationmanager lm =(locationmanager) getsystemservice(location_service); lm.requestlocationupdates(lm.gps_provider, 0,0, loclist); location loc = lm.getlastknownlocation(lm.gps_provider); if(loc==null){ display.settext("no gps location found"); } else{ //set current latitude , longitude currentlon=loc.getlongitude(); currentlat=loc.getlatitude(); } //set last latitude , longitude lastlat=currentlat; lastlon=currentlon ; } locationlistener loclist = new locationlistener(){ @override public void onlocationchanged(location location) { // todo auto-generated method stub //start location manager locationmanager lm =(locationmanager) getsystemservice(location_service); //get last location location loc = lm.getlastknownlocation(lm.gps_provider); //request new location lm.requestlocationupdates(lm.gps_provider, 0,0, loclist); //get new location location loc2 = lm.getlastknownlocation(lm.gps_provider); //get current lat , long currentlat = loc.getlatitude(); currentlon = loc.getlongitude(); location locationa = new location("point a"); locationa.setlatitude(lastlat); locationa.setlongitude(lastlon); location locationb = new location("point b"); locationb.setlatitude(currentlat); locationb.setlongitude(currentlon); double distancemeters = locationa.distanceto(locationb); double distancekm = distancemeters / 1000f; display.settext(string.format("%.2f km",distancekm )); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @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 } }; }
but don't know latitude , longitude put "endlat" , "ednlon".
endlat, endlon confusing name:
you should have lastlat, lastlon: lat,lon received in previous call of ongpsupdate, save e.g distancecalculator.lastlat distancecalculator object store lastlat, , lastlon
then should have curlat, curlon: current lat, lon delivered in ongpsupdate
then calculate distance (lastlat, lastlon) -> (curlat, curlon), , update distancecalculator.lastlat , lastlon.
Comments
Post a Comment