谷歌地图v2 - 绘制不同颜色的路线 - Android

时间:2017-11-21 12:22:25

标签: java android google-maps location polyline

我正在构建一个基于位置的Android应用程序,我想根据他的速度绘制用户所需的路线。 例如:   0-10kmh - 黄色折线   10-20kmh - 红色折线

现在我在后台服务中运行所有内容并更新Map Activity;

位置服务:

if (location.getSpeed() >0 && location.getSpeed < 10) {

           yellowPolyLineArray.add(new LatLng(location.getLatitude(), location.getLongitude()));
            polylineIntent.putParcelableArrayListExtra("yellow",yellowPolyLineArray);



 }else if (location.getSpeed() >10 && location.getSpeed < 20) {

           redPolyLineArray.add(new LatLng(location.getLatitude(), location.getLongitude()));
            polylineIntent.putParcelableArrayListExtra("red",redPolyLineArray);
}

这都是在onLocationChanged方法中完成的,每次用户移动时,我都会使用本地广播接收器发送数组列表来映射活动:

private BroadcastReceiver mDrawRouteRec = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {



            //Receiver array of yellowspeed polylines
            yellowPolyline = intent.getParcelableArrayListExtra("yellow");
            if(yellowPolyline != null){

                //Implement polyline options variable and draw as user moves
                PolylineOptions polylineOptions = new PolylineOptions();
                polylineOptions.addAll(yellowPolyLine);
                polylineOptions
                        .width(5)
                        .color(Color.YELLOW);
                mMap.addPolyline(polylineOptions);
            }


          //Receiver array of red speed polylines
            redPolyline = intent.getParcelableArrayListExtra("yellow");
            if(redPolyline != null){

                //Implement polyline options variable and draw as user moves
                PolylineOptions polylineOptions = new PolylineOptions();
                polylineOptions.addAll(redPolyLine);
                polylineOptions
                        .width(5)
                        .color(Color.RED);
                mMap.addPolyline(polylineOptions);
            }

在用户移动时实时绘制折线

如果用户以15kmh的速度行驶一段距离,然后以5kmh的距离行驶一段距离,那么现在一切正常。

但如果用户以15kmh(黄色)行驶,那么距离为5kmh(红色),然后再行驶15kmh(黄色)。第2个15kmh部分的折线不是从绿色折线结束的地方开始,而是从第1个黄色折线结束的地方开始。导致地图上的一条线。

照片: Screenshot

当我开始绘制红线时,一个想法是清除黄线阵列。但是,还有另一种更有效的解决方案吗?

1 个答案:

答案 0 :(得分:2)

要绘制带有彩色线段的​​折线,您可以使用以下方法:

private void showPolyline(List<ColoredPoint> points) {

    if (points.size() < 2)
        return;

    int ix = 0;
    ColoredPoint currentPoint  = points.get(ix);
    int currentColor = currentPoint.color;
    List<LatLng> currentSegment = new ArrayList<>();
    currentSegment.add(currentPoint.coords);
    ix++;

    while (ix < points.size()) {
        currentPoint = points.get(ix);

        if (currentPoint.color == currentColor) {
            currentSegment.add(currentPoint.coords);
        } else {
            currentSegment.add(currentPoint.coords);
            mGoogleMap.addPolyline(new PolylineOptions()
                    .addAll(currentSegment)
                    .color(currentColor)
                    .width(20));
            currentColor = currentPoint.color;
            currentSegment.clear();
            currentSegment.add(currentPoint.coords);
        }

        ix++;
    }

    mGoogleMap.addPolyline(new PolylineOptions()
            .addAll(currentSegment)
            .color(currentColor)
            .width(20));

}

其中ColoredPoint是:

class ColoredPoint {
    public LatLng coords;
    public int color;

    public ColoredPoint(LatLng coords, int color) {
        this.coords = coords;
        this.color = color;
    }
}

mGoogleMapGoogleMap个对象。对于

List<ColoredPoint> sourcePoints = new ArrayList<>();
sourcePoints.add(new ColoredPoint(new LatLng(-35.27801,149.12958), Color.GREEN));
sourcePoints.add(new ColoredPoint(new LatLng(-35.28032,149.12907), Color.GREEN));
sourcePoints.add(new ColoredPoint(new LatLng(-35.28099,149.12929), Color.YELLOW));
sourcePoints.add(new ColoredPoint(new LatLng(-35.28144,149.12984), Color.YELLOW));
sourcePoints.add(new ColoredPoint(new LatLng(-35.28194,149.13003), Color.RED));
sourcePoints.add(new ColoredPoint(new LatLng(-35.28282,149.12956), Color.RED));
sourcePoints.add(new ColoredPoint(new LatLng(-35.28302,149.12881), Color.BLUE));
sourcePoints.add(new ColoredPoint(new LatLng(-35.28473,149.12836), Color.BLUE));

showPolyline(sourcePoints);
你有类似的东西:

Colored polyline

此外,要将速度转换为颜色,您可以使用以下方法:

public int speedToColor(float speed) {
    int color = Color.TRANSPARENT;
    if (speed < 5) {
        color = Color.BLUE;
    } else if (speed < 25) {
        color = Color.GREEN;
    } else if (speed < 50) {
        color = Color.YELLOW;
    } else {
        color = Color.RED;
    }
    return color;
}
相关问题