i'm making android application should able retrieve user's correct location, , put in hashmap show route in different layout.
anyway question is, why location manager not change google map location when location changes, can see point changes right position, camera not move away, location shown in center of screen image, please tell me why?
the plan show route i've gone on mapso see 1 polyline old position new position, first camera move onto correct position
here code:
public class runningactivity extends fragmentactivity implements locationlistener{ // googlemap screen private googlemap runningmap; private locationmanager lm; // stop watch private chronometer mchronometer; private double longitude,latitude; location lastlocationloc=null; // start & stop time private long starttime,elapsedtime; // used in timer private boolean runningison = false; private thread timer; button start,stop; //saving position in map map<integer, routeobject> map = new hashmap<integer, routeobject>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if(isgoogleplay()){ setcontentview(r.layout.running); setupmap(); } // connect chronometer show user stopwatch mchronometer = (chronometer) findviewbyid(r.id.chronometer_running); // watch button clicks. start = (button)findviewbyid(r.id.startrunningbtn); start.setonclicklistener(mstartlistener); stop = (button)findviewbyid(r.id.stoprunningbtn); stop.setonclicklistener(mstoplistener); runningmap.setmylocationenabled(true); runningmap.setonmaplongclicklistener(myonmaplongclicklistener); timer = new thread(){ public void run(){ try{ int count = 1; // while user has not pressed stop button while(runningison){ // sleeps 5 seconds sleep(5000); // put object in map routeobject object = new routeobject(count,((double)system.nanotime() / 1000000000.0), longitude,latitude); map.put(count,object); // count objects give them name. count++; } } catch(interruptedexception e){ e.printstacktrace(); } // write test logcat routeobject.getmap(map); } }; } onmaplongclicklistener myonmaplongclicklistener = new onmaplongclicklistener() { @override public void onmaplongclick(latlng point) { runningmap.addmarker(new markeroptions().position(point).title( point.tostring())); location mylocation = runningmap.getmylocation(); if (mylocation == null) { toast.maketext(getapplicationcontext(), "my location not available", toast.length_long).show(); } else { polylineoptions polylineoptions = new polylineoptions(); polylineoptions.add(point); polylineoptions.add(new latlng(mylocation.getlatitude(), mylocation.getlongitude())); runningmap.addpolyline(polylineoptions); } } }; view.onclicklistener mstartlistener = new onclicklistener() { public void onclick(view v) { // starting chronometer shown on user screen mchronometer.setbase(systemclock.elapsedrealtime()); mchronometer.start(); // starting real timing used in database starttime = system.nanotime(); // setting value, used in thread runningison = true; // starting thread timer.start(); stop.setbackgroundresource(r.drawable.stopon); start.setbackgroundresource(r.drawable.recordpause); } }; view.onclicklistener mstoplistener = new onclicklistener() { public void onclick(view v) { // thread should stopped runningison = false; mchronometer.stop(); stop.setbackgroundresource(r.drawable.stopoff); start.setbackgroundresource(r.drawable.recordon); elapsedtime = system.nanotime() - starttime; // seconds double seconds = (double)elapsedtime / 1000000000.0; // format result 4 decimals decimalformat df = new decimalformat("0.0000"); // screen system.out.println("elapsed time: "+ df.format(seconds)); // database system.out.println("elapsed time: "+ seconds); toast.maketext(runningactivity.this,df.format(seconds), toast.length_long).show(); gson gson = new gson(); string list = gson.tojson(map); intent intent = new intent(runningactivity.this, routesactivity.class); intent.putextra("your_map", list); startactivity(intent); } }; @override public boolean onoptionsitemselected(menuitem item) { if(item.getitemid() == r.id.action_legalnotices){ startactivity(new intent(this,legalnoticesactivity.class)); } return super.onoptionsitemselected(item); } @override public void onlocationchanged(location location) { runningmap.setmaptype(4); if(lastlocationloc == null){ lastlocationloc = location; } latlng lastlatlng= locationtolatlng(lastlocationloc); latlng thislatlng= locationtolatlng(location); polylineoptions polylineoptions = new polylineoptions(); polylineoptions.add(thislatlng); polylineoptions.add(lastlatlng); runningmap.addpolyline(polylineoptions); lastlocationloc = location; latlng latlng = new latlng(location.getlatitude(),location.getlongitude()); cameraupdate center=cameraupdatefactory.newlatlng(latlng); cameraupdate zoom=cameraupdatefactory.zoomto(17); runningmap.movecamera(center); runningmap.animatecamera(zoom); string message = string.format("your new location found at: \n longitude: %1$s \n latitude: %2$s",location.getlongitude(), location.getlatitude()); toast.maketext(runningactivity.this, message, toast.length_long).show(); lm.removeupdates(this); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub toast.maketext(runningactivity.this,"provider disabled user. gps turned off",toast.length_long).show(); } @override public void onproviderenabled(string provider) { // todo auto-generated method stub toast.maketext(runningactivity.this,"provider enabled user. gps turned on",toast.length_long).show(); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub toast.maketext(runningactivity.this,"provider status changed",toast.length_long).show(); } private void setupmap() { if (runningmap == null) { runningmap = ((supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.running_map)).getmap(); if (runningmap != null) { runningmap.setmylocationenabled(true); runningmap.animatecamera(cameraupdatefactory.zoomby(17)); lm = (locationmanager) getsystemservice(location_service); string provider = lm.getbestprovider(new criteria(), true); if (provider == null) { // ask user turn on gps onproviderdisabled(provider); } location loc = lm.getlastknownlocation(provider); if (loc != null) { onlocationchanged(loc); } } } } private boolean isgoogleplay(){ int status = googleplayservicesutil.isgoogleplayservicesavailable(this); if(status == connectionresult.success){ return(true); }else{ //google play services own error mesage //((dialog)googleplayservicesutil.geterrordialog(status,this, 10)).show(); toast.maketext(this,"google play not available", toast.length_short).show(); } return(false); } public static latlng locationtolatlng(location loc) { if(loc != null) return new latlng(loc.getlatitude(), loc.getlongitude()); return null; }
}
the solution here, activity writes line last location new location, when startbutton pushed. here code. code makes polyline every 5 second. hope of can use it, if making google map application.
public class runningactivity extends fragmentactivity implements locationlistener, onmylocationchangelistener { private googlemap map; private locationmanager locationmanager; private static final long min_time = 5000; private static final float min_distance = 5; // stop watch private chronometer mchronometer; private double longitude, latitude; location lastlocationloc = null; // start & stop time private long starttime, elapsedtime; // used in timer private boolean runningison = false; private thread timer; button start, stop; // saving position in map map<integer, routeobject> hashmap = new hashmap<integer, routeobject>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.running); map = ((supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.running_map)).getmap(); map.setmaptype(4); map.setmylocationenabled(true); locationmanager = (locationmanager) getsystemservice(context.location_service); string provider = locationmanager.getbestprovider(new criteria(), true); locationmanager.requestlocationupdates(provider, min_time, min_distance, this); timer = new thread() { public void run() { try { int count = 1; // while user has not pressed stop button while (runningison) { // sleeps 5 seconds sleep(5000); // put object in map routeobject object = new routeobject(count, ((double) system.nanotime() / 1000000000.0), longitude, latitude); hashmap.put(count, object); // count objects give them name. count++; } } catch (interruptedexception e) { e.printstacktrace(); } // write test logcat routeobject.getmap(hashmap); } }; // connect chronometer show user stopwatch mchronometer = (chronometer) findviewbyid(r.id.chronometer_running); // watch button clicks. start = (button) findviewbyid(r.id.startrunningbtn); start.setonclicklistener(mstartlistener); stop = (button) findviewbyid(r.id.stoprunningbtn); stop.setonclicklistener(mstoplistener); map.setonmylocationchangelistener(this); } view.onclicklistener mstartlistener = new onclicklistener() { public void onclick(view v) { // starting chronometer shown on user screen mchronometer.setbase(systemclock.elapsedrealtime()); mchronometer.start(); // starting real timing used in database starttime = system.nanotime(); // setting value, used in thread runningison = true; // starting thread timer.start(); stop.setbackgroundresource(r.drawable.stopon); start.setbackgroundresource(r.drawable.recordpause); } }; view.onclicklistener mstoplistener = new onclicklistener() { public void onclick(view v) { // thread should stopped runningison = false; mchronometer.stop(); stop.setbackgroundresource(r.drawable.stopoff); start.setbackgroundresource(r.drawable.recordon); elapsedtime = system.nanotime() - starttime; // seconds double seconds = (double) elapsedtime / 1000000000.0; // format result 4 decimals decimalformat df = new decimalformat("0.0000"); // screen system.out.println("elapsed time: " + df.format(seconds)); // database system.out.println("elapsed time: " + seconds); toast.maketext(runningactivity.this, df.format(seconds), toast.length_long).show(); } }; @override public void onmylocationchange(location lastknownlocation) { changemaplocation(lastknownlocation); } private void changemaplocation(location location) { if (lastlocationloc == null) { lastlocationloc = location; } latlng lastlatlng = new latlng(lastlocationloc.getlatitude(),lastlocationloc.getlongitude()); latlng thislatlng = new latlng(location.getlatitude(),location.getlongitude()); // set variables hashmap longitude = location.getlongitude(); latitude = location.getlatitude(); if(runningison) { map.addpolyline(new polylineoptions() .add(thislatlng,lastlatlng) .width(2) .color(color.red)); } map.animatecamera(cameraupdatefactory.newlatlngzoom(thislatlng, 18),null); string old_message = string.format("my old location: \n longitude: %1$s \n latitude: %2$s",lastlocationloc.getlongitude(),lastlocationloc.getlatitude()); toast.maketext(runningactivity.this, old_message,toast.length_long).show(); string message = string.format("my new location: \n longitude: %1$s \n latitude: %2$s",location.getlongitude(),location.getlatitude()); toast.maketext(runningactivity.this, message,toast.length_long).show(); lastlocationloc = location; locationmanager.removeupdates(this); } @override public void onlocationchanged(location location) { } @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 }
}
Comments
Post a Comment