在Android,Google Maps V2中绘制实时路线 [英] Drawing a real time route in android, google maps v2

查看:87
本文介绍了在Android,Google Maps V2中绘制实时路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种更好的方法来实时在android的google map v2上绘制路线.我正在开发android路线跟踪应用程序,所以基本上我有一个连续提供的服务 在后台跟踪我的位置,并通过广播向我的活动发送带有地图片段的位置更新.在活动中,我实现了本地广播接收器,该接收器从服务接收位置更新.我的代码可用于绘制路线,但这并不是最明智的方法,因为我必须不断清除地图,以免路线自身画图.使用Maps v2是否有更好,更有效的方法?

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

// ...
}

我最终使用了setPoints!

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

推荐答案

方法addPolyline返回Polyline对象,您可以在随后的调用中使用setPoints方法设置完整路线(包括新位置).如果路线是地图上的唯一元素,我不知道这是否会带来很大的不同.但是如果你有其他标记肯定比总是清除整个地图要好.

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.

这篇关于在Android,Google Maps V2中绘制实时路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆