如何动态地添加位置指向Android的GoogleMap的一个折线? [英] How do I dynamically add location points to a Polyline in Android GoogleMap?

查看:184
本文介绍了如何动态地添加位置指向Android的GoogleMap的一个折线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我监视当前的位置并注册 GoogleMap.MyLocationChangeListener 。我想画折线(赛道地图)的重新presents我自己的路线。上的每个位置更新,我想新点添加到路线,以便在地图上的轨道被更新。

I monitor current location and register GoogleMap.MyLocationChangeListener. I want to draw a polyline (track on the map) that represents my own route. On each location update, I'd like to add new point to the route, so that the track on the map is updated.

下面是我的code不工作:

Here is my code that doesn't work:

private GoogleMap mMap;
private boolean drawTrack = true;
private Polyline route = null;
private PolylineOptions routeOpts = null;

private void startTracking() {
    if (mMap != null) {
        routeOpts = new PolylineOptions()
                .color(Color.BLUE)
                .width(2 /* TODO: respect density! */)
                .geodesic(true);
        route = mMap.addPolyline(routeOpts);
        route.setVisible(drawTrack);

        mMap.setOnMyLocationChangeListener(this);
    }
}

private void stopTracking() {
    if (mMap != null)
        mMap.setOnMyLocationChangeListener(null);

    if (route != null)
        route.remove();
        route = null;
    }
    routeOpts = null;
}

public void onMyLocationChange(Location location) {
    if (routeOpts != null) {
        LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude());
        routeOpts.add(myLatLng);
    }
}

如何添加点的折线,这样的变化将在用户界面中得到体现?眼下折线不会被渲染。

How to add points to the polyline, so that the changes will be reflected in the UI? Right now the polyline is not rendered.

我使用的是最新的播放服务:71年6月1日(截至此日期)

I am using the latest play-services:6.1.71 (as of this date).

推荐答案

这似乎为我工作:

public void onMyLocationChange(Location location) {
    if (routeOpts != null) {
        LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude());
        List<LatLng> points = route.getPoints();
        points.add(myLatLng);
        route.setPoints(points);
    }
}

有没有更好的办法?

Is there a better way?

这篇关于如何动态地添加位置指向Android的GoogleMap的一个折线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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