Drawing a real time route in android, google maps v2

你离开我真会死。 提交于 2019-12-11 13:06:56

问题


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?

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 up 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);
}

回答1:


The method addPolyline returns a Polyline object, where you can use the method setPoints in subsequent calls, to set the full route (including your new location). I don't know wether this makes much difference if the route is the only element on the map. But if you have e.g. additional markers this is for sure better than always clearing the whole map.



来源:https://stackoverflow.com/questions/23702740/drawing-a-real-time-route-in-android-google-maps-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!