如何在Android设备上显示移动轨迹 [英] How to display a moving track on Android device

查看:892
本文介绍了如何在Android设备上显示移动轨迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to plot my track using GPS on an Android device.

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

I have no problem displaying a completed route but am finding it difficult to show the track as I'm moving.

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

So far, I've found 2 different ways to do that but neither are particularly satisfactory.

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

即建立折线,然后再添加新坐标,然后再添加回去.

ie build up the polyline removing it before adding the new coordinates and then adding it back.

这太慢了.

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

即绘制一系列单条折线.

ie plot a series of single polylines.

虽然此方法的渲染速度比方法1快得多,但看起来参差不齐,尤其是在较低的缩放级别上,因为每条折线都是正方形的,只有实际接触的角.

Whilst this renders a lot faster than Method 1, it looks quite jagged, particularly at lower zoom levels because each polyline is squared off and it is only the corners that actually touch.

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

Is there a better way and, if so, what is it?

推荐答案

有一个使用2.0 Maps API的简单解决方案.您将通过以下三个步骤获得一条流畅的路线:

There's a straightforward solution using the 2.0 Maps API. You'll get a nice smooth route line using three steps:

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

  1. create a list of LatLng points such as:

List<LatLng> routePoints;

  • 将路线点添加到列表中(可以/应该循环执行):

  • Add the route points to the list (could/should be done in a loop):

    routePoints.add(mapPoint);
    

  • 创建一条折线,并将其作为LatLng点列表的来源,例如:

  • Create a Polyline and feed it the list of LatLng points as such:

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

  • 试试看!

    这篇关于如何在Android设备上显示移动轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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