How to get the updated location with an interval of 1 minute ANDROID -
i want updated location interval of 1 minute me put marker on map , @ same other application can see current location whenever move.
so here's code sad won't update location if code inside onlocationchanged
method. i'm not sure though if code's right.
@override public void onlocationchanged(location location) { mlastlocation = location; if (mcurrlocationmarker != null) { mcurrlocationmarker.remove(); } latlng = new latlng(location.getlatitude(), location.getlongitude()); lat = string.valueof(location.getlatitude()); llong = string.valueof(location.getlongitude()); driverid = pref.getstring("driverid", ""); final query username = ref.orderbychild("username").equalto(username); username.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot snapshot : datasnapshot.getchildren()) { if (!snapshot.child("latitude").equals(lat) || !snapshot.child("longitude").equals(llong)) { snapshot.getref().child("latitude").setvalue(lat); snapshot.getref().child("longitude").setvalue(llong); snapshot.getref().child("currentlocation").setvalue(strorigin); toast.maketext(mapsactivity.this, "old lat: " + snapshot.child("latitude").getvalue().tostring() + "\nnew lat: " + lat + "\nold long: " + snapshot.child("longitude").getvalue().tostring() + "\nnew long: " + llong, toast.length_long).show(); } } markeroptions = new markeroptions(); markeroptions.position(latlng); markeroptions.title("current location"); markeroptions.icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_magenta)); mcurrlocationmarker = mmap.addmarker(markeroptions); //move map camera mmap.movecamera(cameraupdatefactory.newlatlng(latlng)); mmap.animatecamera(cameraupdatefactory.zoomto(11)); } @override public void oncancelled(firebaseerror firebaseerror) { toast.maketext(mapsactivity.this, "on cancelled child latlng: " + firebaseerror.getmessage(), toast.length_short).show(); } }); geocoder geocoder; list<android.location.address> addresses; geocoder = new geocoder(this, locale.getdefault()); stringbuilder result = new stringbuilder(); try { addresses = geocoder.getfromlocation(location.getlatitude(), location.getlongitude(), 1); if (addresses.size() > 0) { address address = addresses.get(0); result.append(address.getaddressline(0)).append(", "); result.append(address.getlocality()).append(", "); result.append(address.getcountryname()); } strorigin = result.tostring(); toast.maketext(mapsactivity.this, "origin" + strorigin, toast.length_short).show(); // onsendorigin(r); } catch (ioexception e) { e.printstacktrace(); } //stop location updates if (mgoogleapiclient != null) { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); }\ }
it looks calling:
locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this);
inside onlocationchanged method. stop location updates after first call onlocationchanged.
try removing line , see if works. should call removelocationupdates when done using location services, or if app gets paused/stopped
Comments
Post a Comment