android - I m calculating distance between 2 points in map in real time as the user walks if destination is fixed -



android - I m calculating distance between 2 points in map in real time as the user walks if destination is fixed -

i m trying calculate distance in real time between 2 co-ordinates user walks. using while loop hangs application. have no thought how rectify it. can suggest alternative it.

package com.ankur.mapdemo; import android.app.activity;import android.app.activity; import android.app.notificationmanager; import android.content.context; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.media.ringtonemanager; import android.net.uri; import android.os.bundle; public class alertreminder extends activity implements oncheckedchangelistener,locationlistener { checkbox cb1,cb2,cb3,cb4,cb5,cb6,cb7; textview tv1,tv2; double currentlatitude=0,currentlongitude=0; float dis = 0; location mostrecent = new location(""); locationmanager locationmanager; locationlistener locationlistener; context context; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.alertreminder); locationmanager = (locationmanager) getsystemservice(context.location_service); /* locationlistener locationlistener = new locationlistener() { public void onlocationchanged(location location) { // called when new location found network location provider. makeuseofnewlocation(location); } public void onstatuschanged(string provider, int status, bundle extras) {} public void onproviderenabled(string provider) {} public void onproviderdisabled(string provider) {} }; */ // register listener location manager receive location updates locationmanager.requestlocationupdates(locationmanager.gps_provider, 1, 1, this); tv2= (textview)findviewbyid(r.id.location1); tv1 = (textview)findviewbyid(r.id.currentlocation); cb1 = (checkbox)findviewbyid(r.id.sjt); cb1.setoncheckedchangelistener(this); cb2 = (checkbox)findviewbyid(r.id.tt); cb2.setoncheckedchangelistener(this); cb3 = (checkbox)findviewbyid(r.id.smv); cb3.setoncheckedchangelistener(this); cb4 = (checkbox)findviewbyid(r.id.mb); cb4.setoncheckedchangelistener(this); cb5 = (checkbox)findviewbyid(r.id.gdn); cb5.setoncheckedchangelistener(this); cb6 = (checkbox)findviewbyid(r.id.cdmm); cb6.setoncheckedchangelistener(this); cb7 = (checkbox)findviewbyid(r.id.allmart); cb7.setoncheckedchangelistener(this); } @override public void onlocationchanged(location location) { // todo auto-generated method stub currentlatitude = location.getlatitude(); currentlongitude = location.getlongitude(); makeuseofnewlocation(location); //newlocation(location); tv1.settext("latitude:" + location.getlatitude() + ", longitude:" + location.getlongitude() + ", distance:" + dis); } private void makeuseofnewlocation(location location1) { // todo auto-generated method stub mostrecent = location1; //currentlatitude = location.getlatitude(); //currentlongitude = location.getlongitude(); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub log.d("latitude","disable"); } @override public void onproviderenabled(string provider) { // todo auto-generated method stub log.d("latitude","enable"); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub log.d("latitude","status"); } @override public void oncheckedchanged(compoundbutton arg0, boolean arg1) { // todo auto-generated method stub int = arg0.getid(); if(i == r.id.allmart) { if(arg1) { double destlat = 12.972887; double destlong = 79.159715; location me = new location(""); location dest = new location(""); me.setlatitude(currentlatitude); me.setlongitude(currentlongitude); dest.setlatitude(destlat); dest.setlongitude(destlong); float dist = me.distanceto(dest); while(dist > 50) { makeuseofnewlocation(mostrecent); //cbskjcbkcb //cbshbcbcbhchj //currentlatitude = l1.getlatitude(); //currentlongitude = l1.getlongitude(); me.setlatitude(mostrecent.getlatitude()); me.setlongitude(mostrecent.getlongitude()); dest.setlatitude(destlat); dest.setlongitude(destlong); dist = me.distanceto(dest); dis = dist; } if(dist <= 50) { notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); uri sounduri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification); notificationcompat.builder mbuilder = new notificationcompat.builder(getapplicationcontext()) .setsmallicon(r.drawable.notify) .setcontenttitle("alert") .setcontenttext("this notification") .setsound(sounduri); notificationmanager.notify(0, mbuilder.build()); } } } }

} `

as long distance greater 50, remain within loop, won't update in layout. create new thread calculation (google multithreading), , allow main thread values , update them in layout.

android google-maps geolocation

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -