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

Java 谷歌地图v2-用不同颜色绘制路线-Android,java,android,google-maps,location,polyline,Java,Android,Google Maps,Location,Polyline,我正在构建一个基于位置的android应用程序,我想根据用户的速度绘制一条路线。 例如: 0-10kmh-黄色多段线 10-20公里小时-红色多段线 现在我在后台运行所有服务,并更新地图活动 定位服务: if (location.getSpeed() >0 && location.getSpeed < 10) { yellowPolyLineArray.add(new LatLng(location.getLatitude(), locatio

我正在构建一个基于位置的android应用程序,我想根据用户的速度绘制一条路线。 例如: 0-10kmh-黄色多段线 10-20公里小时-红色多段线

现在我在后台运行所有服务,并更新地图活动

定位服务:

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);
}
多段线是在用户移动时实时绘制的

现在,如果用户以15公里小时的速度行驶一段距离,然后以5公里小时的速度行驶一段距离,这一切都很好

但如果用户以15公里小时(黄色)的速度行驶,则以5公里小时(红色)的速度行驶一段距离,然后再以15公里小时(黄色)的速度行驶。第二条15kmh截面的多段线不是从绿色多段线结束的位置开始,而是从第一条黄色多段线结束的位置开始。在地图上划出一条线

图片:


一个想法是在我开始画红线时清除黄线阵列。但还有其他更有效的解决方案吗?

要用彩色线段绘制多段线,您可以使用以下方法:

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

}
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;
}
mGoogleMap
GoogleMap
对象。为了

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);
List sourcePoints=new ArrayList();
sourcePoints.add(新颜色点(新板条(-35.27801149.12958),颜色为绿色);
sourcePoints.add(新着色点(新板条(-35.28032149.12907),颜色为绿色);
sourcePoints.add(新颜色点(新板条(-35.28099149.12929),颜色为黄色);
sourcePoints.add(新颜色点(新板条(-35.28144149.12984),颜色为黄色);
sourcePoints.add(新颜色点(新板条(-35.28194149.13003),颜色为红色);
sourcePoints.add(新颜色点(新板条(-35.282822149.12956),颜色为红色);
sourcePoints.add(新颜色点(新板条(-35.28302149.12881),颜色为蓝色);
sourcePoints.add(新颜色点(新板条(-35.28473149.12836),颜色为蓝色);
显示多段线(源点);
你得到了类似于:

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

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

}
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;
}
public int speedToColor(浮动速度){
int color=color.TRANSPARENT;
如果(速度<5){
颜色=颜色。蓝色;
}否则,如果(速度<25){
颜色=颜色。绿色;
}否则,如果(速度<50){
颜色=颜色。黄色;
}否则{
颜色=颜色。红色;
}
返回颜色;
}

要使用彩色线段绘制多段线,可以使用以下方法:

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

}
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;
}
mGoogleMap
GoogleMap
对象。为了

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);
List sourcePoints=new ArrayList();
sourcePoints.add(新颜色点(新板条(-35.27801149.12958),颜色为绿色);
sourcePoints.add(新着色点(新板条(-35.28032149.12907),颜色为绿色);
sourcePoints.add(新颜色点(新板条(-35.28099149.12929),颜色为黄色);
sourcePoints.add(新颜色点(新板条(-35.28144149.12984),颜色为黄色);
sourcePoints.add(新颜色点(新板条(-35.28194149.13003),颜色为红色);
sourcePoints.add(新颜色点(新板条(-35.282822149.12956),颜色为红色);
sourcePoints.add(新颜色点(新板条(-35.28302149.12881),颜色为蓝色);
sourcePoints.add(新颜色点(新板条(-35.28473149.12836),颜色为蓝色);
显示多段线(源点);
你得到了类似于:

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

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

}
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;
}
public int speedToColor(浮动速度){
int color=color.TRANSPARENT;
如果(速度<5){
颜色=颜色。蓝色;
}否则,如果(速度<25){
颜色=颜色。绿色;
}否则,如果(速度<50){
颜色=颜色。黄色;
}否则{
颜色=颜色。红色;
}
返回颜色;
}

而不是维护黄色和红色的两个列表。保持一个单一的列表,并提及要绘制的颜色,并相应地绘制线。但我如何区分何时绘制黄色线,何时绘制红色线,因为只有一个坐标列表。我曾考虑使用HashMap,但它不起作用。您可以创建用于存储
纬度、经度和颜色的自定义类。无论何时,当你在任何条件下存储颜色并根据它绘制。嗯,好主意,所以每次我的位置改变时,我都会用新值覆盖自定义类,将其发送到Map Activity并绘制它?准确地说。此外,您不需要传递列表。您只需将一个对象传递给该对象,而无需维护黄色和红色的两个列表。保持一个单一的列表,并提及要绘制的颜色,并相应地绘制线。但我如何区分何时绘制黄色线,何时绘制红色线,因为只有一个坐标列表。我曾考虑使用HashMap,但它不起作用。您可以创建用于存储
纬度、经度和颜色的自定义类。无论何时,当你在任何条件下存储颜色并根据它绘制。嗯,好主意,所以每次我的位置改变时,我都会用新值覆盖自定义类,将其发送到Map Activity并绘制它?准确地说。此外,您不需要传递列表。你可以把一个物体传给它。