How to get GPS Distance travelled in Android with this code? -
i totally new android , stuck problem.
basically want when move, app gps info , display on phone including distance travelled in meter. don't know how in android.
this code.
public class mainactivity extends appcompatactivity { textview tv; textview tv2; togglebutton tb; button currentgps; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); location location11 = null; double longitude1 = 0;//location11.getlongitude(); double latitude1 = 0;//location11.getlatitude(); tv = (textview) findviewbyid(r.id.textview2); tv.settext("ready"); tb = (togglebutton)findviewbyid(r.id.toggle1); final locationmanager lm = (locationmanager) getsystemservice(context.location_service); tb.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { try{ if(tb.ischecked()){ tv.settext("connecting.."); lm.requestlocationupdates(locationmanager.gps_provider, 100, 1, mlocationlistener); lm.requestlocationupdates(locationmanager.network_provider, 100, 1, mlocationlistener); }else{ tv.settext("information not connecting"); lm.removeupdates(mlocationlistener); } }catch(securityexception ex){ } } }); } private final locationlistener mlocationlistener = new locationlistener() { public void onlocationchanged(location location1) { log.d("test", "onlocationchanged, location:" + location1); double longitude = location1.getlongitude(); double latitude = location1.getlatitude(); double altitude = location1.getaltitude(); float accuracy = location1.getaccuracy(); string provider = location1.getprovider(); final double latitudea = location1.getlatitude(); final double longitudea = location1.getlongitude(); double distance; location locationa = new location("point a"); locationa.setlatitude(latitudea); locationa.setlongitude(longitudea); location locationb = new location("point b"); locationb.setlatitude(latitude); locationb.setlongitude(longitude); distance = locationa.distanceto(locationb); float currentspeed = (location1.getspeed()*3600/1000); string convertedspeed = string.format("%.2f",currentspeed); tv.settext("provider : " + provider + "\n\nlatitude : " + longitude + "\n\nlangitude : " + latitude + "\n\naltitude : " + altitude + "\n\naccuracy : " + accuracy +"/\n\nmoving distance : " + distance + "/m" + "\n\ncurrent speed : " + convertedspeed + "km/h"); } public void onproviderdisabled(string provider) { log.d("test", "onproviderdisabled, provider:" + provider); } public void onproviderenabled(string provider) { log.d("test", "onproviderenabled, provider:" + provider); } public void onstatuschanged(string provider, int status, bundle extras) { log.d("test", "onstatuschanged, provider:" + provider + ", status:" + status + " ,bundle:" + extras); } }; }
Comments
Post a Comment