【问题标题】:How to display a moving track on Android device如何在 Android 设备上显示移动轨迹
【发布时间】:2013-03-26 15:57:43
【问题描述】:

我想在 Android 设备上使用 GPS 绘制我的轨迹。

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

到目前为止,我已经找到了 2 种不同的方法来做到这一点,但都不是特别令人满意。

方法一

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

即在添加新坐标之前构建折线,然后将其添加回来。

这太慢了。

方法 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;
}

即绘制一系列单折线。

虽然这种渲染速度比方法 1 快很多,但它看起来非常参差不齐,尤其是在较低的缩放级别下,因为每条折线都是方形的,并且只有角实际接触。

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

【问题讨论】:

    标签: google-maps-api-2 android-sdk-2.3 polyline


    【解决方案1】:

    使用 2.0 Maps API 有一个简单的解决方案。使用三个步骤,您将获得一条漂亮的平滑路线:

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

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

      routePoints.add(mapPoint);
      
    3. 创建一条折线并将 LatLng 点列表输入如下:

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

    试一试!

    【讨论】:

    • 感谢您的回答。我最终自己想出了这个(我的方法1)。我遇到的问题是,当我遍历坐标的 ArrayList 构造和删除折线时,处理将永远进行。但是,当我将该处理卸载到 onChangeListener 时,它就像一个魅力。
    • @PeteH 能否请您告诉我,方法 routePoints.add(mapPoint); 中的 mapPoint 是什么?
    • a mapPoint 只是一个单独的 LatLng 对象,即地图显示所需的点之一
    猜你喜欢
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多