android - jumping location on google maps by gps. (inaccurate distance) -
my app should display distance , location in real time when user walked, there errors output. distance did not start when start increase lot until slows down bit. red marker , blue dot should according 1 example i've seen separate in app. both of them point current location right? both red marker , blue dot jumps around in short time not moving away true location. believe lead inaccuracy of distance well.
please see output more details
java code
public class mainactivity extends fragmentactivity implements locationlistener{ protected locationmanager locationmanager; private googlemap googlemap; button btnstartmove,btnpause,btnresume,btnstop; static double n=0; long s1,r1; double dis=0.0; thread t1; edittext usernumberinput; boolean bool=false; int count=0; double speed = 1.6; double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4; double dist = 0; textview distancetext; float[] result; private static final long minimum_distance_change_for_updates =1; // in meters private static final long minimum_time_between_updates = 3000; //in milliseconds boolean startdistance = false; boolean firsttime = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); locationmanager = (locationmanager) getsystemservice(location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider,minimum_time_between_updates,minimum_distance_change_for_updates, this); if(isgoogleplay()) { setupmapifneeded(); } distancetext=(textview)findviewbyid(r.id.distance); btnstartmove=(button)findviewbyid(r.id.start);//start moving //prepare distance........... log.d("gps enabled", "gps enabled"); criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_fine); string provider = locationmanager.getbestprovider(criteria, true); location location=locationmanager.getlastknownlocation(provider); btnstartmove.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { startdistance = true; // lat3 = location.getlatitude(); // lon3 = location.getlongitude(); } }); if(location!= null) { //display current location in toast string message = string.format( "current location \n longitude: %1$s \n latitude: %2$s", location.getlongitude(), location.getlatitude() ); toast.maketext(mainactivity.this, message, toast.length_long).show(); //display current location in textview //latitude.settext("current latitude: " + string.valueof(location.getlatitude())); //longitude.settext("current longitude: " + string.valueof(location.getlongitude())); //lat3 = location.getlatitude(); //lon3 = location.getlongitude(); } else if(location == null) { toast.maketext(mainactivity.this, "location null", toast.length_long).show(); } } private void setupmapifneeded() { if(googlemap == null) { toast.maketext(mainactivity.this, "getting map", toast.length_long).show(); googlemap =((supportmapfragment)getsupportfragmentmanager().findfragmentbyid(r.id.displaymap)).getmap(); if(googlemap != null) { setupmap(); } } } private void setupmap() { //enable mylocation layer of google map googlemap.setmylocationenabled(true); //get locationmanager object system service location_service //locationmanager locationmanager = (locationmanager) getsystemservice(location_service); //create criteria object retrieve provider criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_fine); //get name of best provider string provider = locationmanager.getbestprovider(criteria, true); if(provider == null) { onproviderdisabled(provider); } //set map type googlemap.setmaptype(googlemap.map_type_normal); //get current location location mylocation = locationmanager.getlastknownlocation(provider); if(mylocation != null) { onlocationchanged(mylocation); } locationmanager.requestlocationupdates(provider, 0, 0, this); } private boolean isgoogleplay() { int status = googleplayservicesutil.isgoogleplayservicesavailable(this); if (status == connectionresult.success) { toast.maketext(mainactivity.this, "google play services available", toast.length_long).show(); return(true); } else { googleplayservicesutil.geterrordialog(status, this, 10).show(); } return (false); } @override public void onlocationchanged(location mylocation) { system.out.println("speed " + mylocation.getspeed()); // if(mylocation.getspeed() > speed) // { //show location on map................. //get latitude of current location double latitude = mylocation.getlatitude(); //get longitude of current location double longitude = mylocation.getlongitude(); //create latlng object current location latlng latlng = new latlng(latitude, longitude); //show current location in google map googlemap.movecamera(cameraupdatefactory.newlatlng(latlng)); //zoom in google map googlemap.animatecamera(cameraupdatefactory.zoomto(20)); googlemap.addmarker(new markeroptions().position(new latlng(latitude, longitude)).title("you here!")); //show distance............................ if(startdistance == true) { toast.maketext(mainactivity.this, "location has changed", toast.length_long).show(); if(mylocation != null) { //latitude.settext("current latitude: " + string.valueof(loc2.getlatitude())); //longitude.settext("current longitude: " + string.valueof(loc2.getlongitude())); float[] results = new float[1]; location.distancebetween(lat3, lon3, mylocation.getlatitude(), mylocation.getlongitude(), results); system.out.println("distance is: " + results[0]); dist += results[0]; decimalformat df = new decimalformat("#.##"); // adjust appropriate if(count==1) { distancetext.settext(df.format(dist) + "meters"); } lat3=mylocation.getlatitude(); lon3=mylocation.getlongitude(); count=1; } } startdistance=true; //} } @override public void onproviderdisabled(string provider) { toast.maketext(mainactivity.this, "provider disabled user. gps turned off", toast.length_long).show(); } @override public void onproviderenabled(string provider) { toast.maketext(mainactivity.this, "provider enabled user. gps turned on", toast.length_long).show(); } @override public void onstatuschanged(string provider, int status, bundle extras) { toast.maketext(mainactivity.this, "provider status changed", toast.length_long).show(); } @override protected void onpause() { super.onpause(); locationmanager.removeupdates(this); } @override protected void onresume() { super.onresume(); locationmanager.requestlocationupdates(locationmanager.gps_provider,minimum_time_between_updates,minimum_distance_change_for_updates, this); }}
there seem no way solve error.i recommeded use location.getspeed returns 0.
Comments
Post a Comment