gpt4 book ai didi

java - Google map v2 - 用不同颜色绘制路线 - Android

转载 作者:行者123 更新时间:2023-12-01 20:05:45 24 4
gpt4 key购买 nike

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

现在我在后台服务中运行所有内容并更新 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 方法中完成的,每次用户移动时,我都会使用本地广播接收器发送数组列表来映射 Activity :

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

随着用户移动实时绘制折线

现在,如果用户以 15 公里/小时的速度行驶一段距离,然后以 5 公里/小时的速度行驶一段距离,则一切正常。

但是,如果用户以 15 公里/小时(黄色)的速度行驶,然后以 5 公里/小时(红色)的速度行驶一段距离,然后再次以 15 公里/小时(黄色)的速度行驶。第二个 15 公里/小时路段的折线不是从绿色折线结束的地方开始,而是从第一条黄色折线结束的地方开始。导致 map 上出现一条线。

图片: Screenshot

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

最佳答案

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

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

关于java - Google map v2 - 用不同颜色绘制路线 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47413037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com