java - Calculating distance between two points -
i know has been asked loads before, i'm getting mixed views on whats correct , whats not,
i trying calculate distance between 2 points in app using 2 buttons, start , stop.
i figured simple, lat , long of position when start pressed, them again when stop pressed, calculate 2 , voila. isn't going plan.
i have set dialog on stop button when clicked feedback , i'm returning (presumably) single latitude or longitude.
my codes below::
public class maprun extends fragmentactivity implements locationlistener, locationsource { private onlocationchangedlistener mlistener; private locationmanager locationmanager; private googlemap mmap; double lat, lng; static double startlat; double startlong; double stoplat; double stoplong; public static location l; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.maprun); locationmanager = (locationmanager) getsystemservice(location_service); // creating criteria object retrieve provider criteria criteria = new criteria(); // getting name of best provider string provider = locationmanager.getbestprovider(criteria, true); // getting current location location location = locationmanager.getlastknownlocation(provider); l = location; lat = l.getlatitude(); lng = l.getlongitude(); onlocationchanged(location); locationmanager.requestlocationupdates(provider, 20000, 0, this); setupmapifneeded(); } @override public void onpause() { if (locationmanager != null) { locationmanager.removeupdates(this); } super.onpause(); } @override public void onresume() { super.onresume(); setupmapifneeded(); if (locationmanager != null) { mmap.setmylocationenabled(true); } } /** * sets map if possible (i.e., google play * services apk correctly installed) , map has not been * instantiated.. ensure ever call * {@link #setupmap()} once when {@link #mmap} not null. * <p> * if isn't installed {@link supportmapfragment} (and * {@link com.google.android.gms.maps.mapview mapview}) show prompt * user install/update google play services apk on * device. * <p> * user can return activity after following prompt , * correctly installing/updating/enabling google play services. since * activity may not have been destroyed during process * (it stopped or paused), * {@link #oncreate(bundle)} may not called again should call * method in {@link #onresume()} guarantee called. */ private void setupmapifneeded() { // null check confirm have not instantiated // map. if (mmap == null) { // try obtain map supportmapfragment. mmap = ((supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map)).getmap(); // check if successful in obtaining map. if (mmap != null) { setupmap(); } // how register locationsource mmap.setlocationsource(this); } } /** * can add markers or lines, add listeners or move * camera. in case, add marker near africa. * <p> * should called once , when sure {@link #mmap} * not null. */ private void setupmap() { mmap.setmylocationenabled(true); } @override public void activate(onlocationchangedlistener listener) { mlistener = listener; } @override public void deactivate() { mlistener = null; } @override public void onlocationchanged(location location) { if (mlistener != null) { mlistener.onlocationchanged(location); // move camera user's location once it's available! mmap.animatecamera(cameraupdatefactory.newlatlngzoom(new latlng( location.getlatitude(), location.getlongitude()), 16.0f)); } } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub toast.maketext(this, "provider disabled", toast.length_short).show(); } @override public void onproviderenabled(string provider) { // todo auto-generated method stub toast.maketext(this, "provider enabled", toast.length_short).show(); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub toast.maketext(this, "status changed", toast.length_short).show(); } @override public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu); menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.map_menu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.startrun: startrun(); case r.id.stoprun: stoprun(); return true; default: return super.onoptionsitemselected(item); } } public void startrun() { ; } public void stoprun() { location startpoint = new location("ran from"); startpoint.setlatitude(lat); startpoint.setlongitude(lng); location stoppoint = new location("ran to"); stoppoint.setlatitude(stoplat); stoppoint.setlongitude(stoplong); float distance = startpoint.distanceto(stoppoint); string diststr = string.valueof(distance); dialog d = new dialog(this); d.settitle("distance"); textview tv = new textview(this); tv.settext(diststr); d.setcontentview(tv); d.show(); } here updated buttons far, i've got principle think i'm struggling correct way store , pass variables:
@override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.startrun: currentlocation = lat + lng; startlocation = currentlocation; case r.id.stoprun: currentlocation = lat + lng; stoplocatio n = currentlocation;
float distance = startlocation.distanceto(stoplocation); string diststr = string.valueof(distance); dialog d = new dialog(this); d.settitle("distance"); textview tv = new textview(this); tv.settext(diststr); d.setcontentview(tv); d.show(); return true; default: return super.onoptionsitemselected(item); } and onlocationchanged:
@override public void onlocationchanged(location location) { l = location; lat = l.getlatitude(); lng = l.getlongitude(); if (mlistener != null) { mlistener.onlocationchanged(location); // move camera user's location once it's available! mmap.animatecamera(cameraupdatefactory.newlatlngzoom(new latlng( location.getlatitude(), location.getlongitude()), 16.0f)); } }
location has distanceto. register location gps, when user press start. when user press stop, current location , do:
currentlocation.distanceto(firstlocation) ; distanceto returns float represent approximate distance in meters between location , given location
public class maprun extends fragmentactivity implements locationlistener, locationsource { location startlocation; location endlocation; location currentlocation; @override public void onlocationchanged(location location) { if (mlistener != null) { mlistener.onlocationchanged(location); // move camera user's location once it's available! mmap.animatecamera(cameraupdatefactory.newlatlngzoom(new latlng( location.getlatitude(), location.getlongitude()), 16.0f)); } currentlocation = location; } public void startrun() { startlocation = currentlocation; } public void stoprun() { stoplocation = currentlocation; } public float getdistance() { float distance = 0f; if (stoplocation != null && startlocation != null) distance = stoplocation.distanceto(startlocation); return distance; } }
Comments
Post a Comment