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

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

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

定位服务 :

if (location.getSpeed() >0 && 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(黄色)。 第二个15kmh部分的折线不是从绿色折线结束的地方开始,而是从第一个黄色折线结束的地方开始。 导致整个地图上的一条线。

图片: 截图

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

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

 private void showPolyline(List points) { if (points.size() < 2) return; int ix = 0; ColoredPoint currentPoint = points.get(ix); int currentColor = currentPoint.color; List 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 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); 

你有类似的东西:

彩色折线

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

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