how to keep redrawing the polyline and markers when locationmanager gives new location on google maps api for android -
the code works fine first time round, when locationmanager requests update doesn't redraw markers or polylines when location changes little blue gps circle on moves self , thats it. want know how change markers , polylines google maps every time user moves new location
public class myactivity extends fragmentactivity implements routinglistener { googlemap map; latlng user; latlng end; marker user; marker end; /** * activity loads map , displays route , pushpins on it. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); try { if (map == null) { supportmapfragment fm = (supportmapfragment) getsupportfragmentmanager(). findfragmentbyid(r.id.map); map = fm.getmap(); if (map != null) { setupmap(); } } } catch (exception e) { e.printstacktrace(); } } private void setupmap() { map.setmylocationenabled(true); locationmanager locationmanager = (locationmanager) getsystemservice(location_service); criteria criteria = new criteria(); string provider = locationmanager.getbestprovider(criteria, true); location mylocation = locationmanager.getlastknownlocation(provider); locationmanager.requestlocationupdates(provider, 5000, 0, (android.location.locationlistener) this); if(locationmanager!=true){ onlocationchanged(mylocation); } } private void onlocationchanged(location mylocation) { user.remove(); end.remove(); double latitude = mylocation.getlatitude(); double longitude = mylocation.getlongitude(); end = new latlng(51.493873, -3.18175); user = new latlng(latitude, longitude); marker user = map.addmarker(new markeroptions(). position(new latlng(latitude, longitude)).title("you").icon(bitmapdescriptorfactory.fromresource(r.drawable.start_blue))); marker end = map.addmarker(new markeroptions(). position(end).title("other user").icon(bitmapdescriptorfactory.fromresource(r.drawable.end_green))); map.movecamera((cameraupdatefactory.newlatlng(user))); map.animatecamera(cameraupdatefactory.zoomto(15)); routing routing = new routing(routing.travelmode.walking); routing.registerlistener(this); routing.execute(user,end); } @override public void onroutingfailure() { // routing request failed } @override public void onroutingstart() { // routing request starts } @override public void onroutingsuccess(polylineoptions mpolyoptions, route route) { polylineoptions polyoptions = new polylineoptions(); polyoptions.color(color.blue); polyoptions.width(10); polyoptions.addall(mpolyoptions.getpoints()); map.addpolyline(polyoptions); }
}
you listening when user has changed location. if want redraw every 5 seconds regardless if user has moved.
using new googleapiclient
private googleapiclient mgoogleapiclient; private static final scheduledexecutorservice delayer = executors.newsinglethreadscheduledexecutor(); @override public void oncreate(bundle savedinstancestate) { mgoogleapiclient = new googleapiclient.builder(this).addapi(locationservices.api).addconnectioncallbacks(this).addonconnectionfailedlistener(this).build(); mgoogleapiclient.connect(); } @override public void onconnected(bundle bundle) { location location; runnable task = new runnable() { public void run() { location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); // location } }; delayer.schedule(task, 5, timeunit.seconds); }
Comments
Post a Comment