Google Maps v2 - 以不同颜色绘制路线 - Android [英] Google Maps v2 - draw route with different colors - Android

查看:131
本文介绍了Google Maps v2 - 以不同颜色绘制路线 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



现在我在背景服务中运行所有内容,我更新地图活动;

定位服务:

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

$ / code>

这些都是在 onLocationChanged 方法,我使用本地广播接收器发送数组列表以映射活动:

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


$ b // // Yellowspeed多段线的接收器数组
yellowPolyline = intent.getParcelableArrayListExtra(yellow);
if(yellowPolyline!= null){

//实现多段线选项变量并以用户移动绘制
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.addAll(yellowPolyLine);
polylineOptions
.width(5)
.color(Color.YELLOW);
mMap.addPolyline(polylineOptions);
}


//红色速度折线的接收器阵列
redPolyline = intent.getParcelableArrayListExtra(yellow);
if(redPolyline!= null){

//实现多段线选项变量并以用户移动绘制
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.addAll(redPolyLine);
polylineOptions
.width(5)
.color(Color.RED);
mMap.addPolyline(polylineOptions);

$ / code>

Polyline在用户移动时实时绘制



现在,如果用户以15公里的速度驾驶一段距离,然后以5公里的速度行驶一段距离,那么这一切都很好。然而,如果用户在15kmh黄色),然后在5kmh(红色)和15kmh(黄色)之间再行一段距离。第二个15kmh部分所在的多段线不是开始于绿色多段线结束的地方,而是开始于第1个黄色多段线结束的位置。导致地图上出现一行。



图片:



此外,为了将速度转换为彩色,您可以使用类似的方法:

  public int speedToColor(float speed){
int color = Color.TRANSPARENT;
if(速度<5){
color = Color.BLUE;
} else if(速度<25){
color = Color.GREEN;
} else if(speed <50){
color = Color.YELLOW;
} else {
color = Color.RED;
}
返回颜色;
}


I am building a location based android app and I want to draw a route user takes based on his speed. For example: 0-10kmh - Yellow polyline 10-20kmh - Red polyline

Now I run everything in background service and I update Map Activity;

Location Service :

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

This is all being done in onLocationChangedmethod and I send array Lists to map activity everytime user moves, using a Local Broadcast receiver:

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

Polyline is being drawn in real time as user moves

Now this works all fine if user drives some distance at 15kmh and then some distance at 5kmh.

But if user drives at 15kmh (yellow) then some distance at 5kmh (red) and then again at 15kmh (yellow). The polyline where 2nd 15kmh section is starts not where a green polyline ended, but it starts where 1st yellow polyline ended. Resulting in a line across the map.

Picture: Screenshot

One Idea was to clear yellow line array when I start drawing red line. But is there another, more efficient solution?

解决方案

To draw polyline with colored segments you can use method like that:

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

}

where ColoredPoint is:

class ColoredPoint {
    public LatLng coords;
    public int color;

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

and mGoogleMap is GoogleMap object. For

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

you got something like:

Also, for convert speed to color, you can use method like that:

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

这篇关于Google Maps v2 - 以不同颜色绘制路线 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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