Drawing a real time route in android, google maps v2 -
i'm looking better way @ drawing route on google map v2 on android in real time. i'm developing android route tracking application, have service continuasly tracks location in background , sends location update via broadcast activity map fragment. in activity have implemented local broadcast receiver receives location updates service. code works @ drawing route not smartest way because have continuasly clear map avoid route overdrawing itself. there better , efficient way maps v2?
public class trackingservice extends service { // ... @override public void onlocationchanged(location location) { //... datasource.open(); datasource.insertlocation(location.getlatitude(), location.getlongitude()) datasource.close(); broadcastlocation(location); } private void broadcastlocation(location location) { intent intent = new intent(action_receive_location); intent.putextra(key_new_location, location); sendbroadcast(intent); } } public class trackingactivity extends activity { private googlemap googlemap; private polylineoptions polylineoptions; private routesdatasource datasource; private intentfilter intentfilter; private broadcastreceiver receiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { googlemap.clear(); location location = intent.getparcelableextra(trackingservice.key_new_location); latlng latlng = new latlng(location.getlatitude(), location.getlongitude()); cameraupdate update = cameraupdatefactory.newlatlngzoom(latlng, camera_zoom); googlemap.animatecamera(update); polylineoptions.add(latlng); googlemap.addpolyline(polylineoptions); } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_tracking); googlemap = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); polylineoptions = new polylineoptions().width(polyline_width).color(color.red); datasource = new routesdatasource(this); intentfilter = new intentfilter(trackingservice.action_receive_location); registerreceiver(receiver, intentfilter); drawrouteoncreate(); } private void drawrouteoncreate() { datasource.open(); list<latlng> locations = datasource.getalllocations(id); datasource.close(); polylineoptions.addall(locations); googlemap.addpolyline(polylineoptions); } @override protected void ondestroy() { unregisterreceiver(receiver); super.ondestroy(); } // ... }
i ended using setpoints!
private polyline route; private list<latlng> points; @override public void onreceive(context context, intent intent) { // ... points.add(latlng)); route.setpoints(points); } private void drawrouteoncreate() { route = map.addpolyline(new polylineoptions().width(6).color(color.red)); dbadapter.open(); points = dbadapter.getalllocations(); dbadapter.close(); route.setpoints(points); }
the method addpolyline returns polyline object, can use method setpoints in subsequent calls, set full route (including new location). don't know wether makes difference if route element on map. if have e.g. additional markers sure better clearing whole map.
Comments
Post a Comment