如何在Android设备上显示移动轨道

Dou*_*ran 6 polyline google-maps-api-2 android-sdk-2.3

我想在Android设备上使用GPS绘制我的曲目.

我没有问题显示完成的路线,但我发现很难在我移动时显示轨道.

到目前为止,我已经找到了两种不同的方法,但两者都不是特别令人满意.

方法1

PolylineOptions track = new PolylineOptions();
Polyline poly;

while (moving) {
    Latlng coord = new LatLng(lat,lng);    // from LocationListener
    track.add(coord);
    if (poly != null) {
        poly.remove();
    }
    poly = map.addPolyline(track);
}
Run Code Online (Sandbox Code Playgroud)

即在添加新坐标然后将其添加回来之前建立折线以将其移除.

这非常缓慢.

方法2

oldcoord = new LatLng(lat,lng);;

while (moving) {
    PolylineOptions track = new PolylineOptions();
    LatLng coord = new (LatLng(lat,lng);
    track.add(oldcoord);
    track.add(coord);
    map.addPolyline(track);

    oldcoord = coord;
}
Run Code Online (Sandbox Code Playgroud)

即绘制一系列单折线.

虽然这比方法1快得多,但它看起来很锯齿,特别是在较低的缩放级别,因为每个折线都是方形的,它只是实际接触的角落.

是否有更好的方法,如果有,它是什么?

Pet*_*teH 8

使用2.0 Maps API有一个简单的解决方案.您将使用三个步骤获得良好的平滑路线:

  1. 创建LatLng点列表,例如:

    List<LatLng> routePoints;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将路线点添加到列表中(可以/应该在循环中完成):

    routePoints.add(mapPoint);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建折线并将其作为LatLng点列表输入:

    Polyline route = map.addPolyline(new PolylineOptions()
      .width(_strokeWidth)
      .color(_pathColor)
      .geodesic(true)
      .zIndex(z));
    route.setPoints(routePoints);
    
    Run Code Online (Sandbox Code Playgroud)

试试看!