title: Drawing a real time route in android, google maps v2
body: <p>I'm looking for a better way at drawing a route on google map v2 on android in real time. I'm developing android route tracking application, so basically i have a service which continuasly 
  tracks my location in background and sends a location update via broadcast to my activity with map fragment. In activity i have implemented local broadcast receiver which receives location updates from service. My code works at drawing a route but it is not the smartest way to do because i have to continuasly clear the map to avoid route overdrawing itself. Is there a better and efficient way to do this with maps v2?</p>
  
  <pre><code>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&lt;LatLng&gt; locations = dataSource.getAllLocations(id);
      dataSource.close();
  
      polylineOptions.addAll(locations);
      googleMap.addPolyline(polylineOptions);
  }
  
  @Override
  protected void onDestroy()
  {
      unregisterReceiver(receiver);
      super.onDestroy();
  }
  
  // ...
  }
  </code></pre>
  
  <p>I ended up using setPoints!</p>
  
  <pre><code>private Polyline route;
  private List&lt;LatLng&gt; 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);
  }
  </code></pre>