动态多段线颜色与Google Maps Android应用程序 [英] dynamic Polyline colors with google maps android app

查看:74
本文介绍了动态多段线颜色与Google Maps Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个使用google地图的应用程序,以显示您何时偏离了先前的路径.我可以用不同的颜色显示发生偏差的位置,但是我必须初始化一个新的PolyOptions(),从而产生断断续续"的折线,而不是像我的基本路线那样平滑的折线.如果我不初始化新的PolyOptions(),那么当我开始偏离时,即使我仅尝试将颜色应用于该特定的折线,它也会将路径的大部分变为红色.当我回到路径时,在偏离后,我失去了偏离的颜色,然后全部变回黑色.谁能向我解释发生了什么以及如何将新的PolyOptions()方法中的正确着色与使用一个PolyLineOptions()获得的平滑度相结合

I'm making an app that uses google maps to show when you deviate from a path previously taken. I can show where the deviations occur with a different color, but I have to initialize a new PolyOptions(), resulting in "choppy" polylines instead of smooth polylines like I have with my base route. If I don't initialize a new PolyOptions() then when I start to deviate, it turns a large portion of the path red, even though I'm only trying to apply the color to that one particular polyline. When I come back to the path, after deviating, I lose the deviated coloring and it all goes back to black. Can anybody explain to me what's going on and how I can combine the proper coloring from the new PolyOptions() method with the smoothness that you get with using one PolyLineOptions()

  1. 新的PolyLineOptions()方法:

for(HaulEvent event:cycle){ LatLng locToAdd = new LatLng(event.Latitude,event.Longitude);

for (HaulEvent event : cycle) { LatLng locToAdd = new LatLng(event.Latitude, event.Longitude);

                    if (event.cycle_num < 2) {

                        m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
                                .color(Color.BLACK)
                                .add(locToAdd)
                                .clickable(true)));
                        m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
                    } else { //after we've got a base cycle drawn
                        if (PolyUtil.isLocationOnPath(locToAdd, m_aBaseRoute, true, tolerance)) {
                            m_aPL.add(mMap.addPolyline(new PolylineOptions().geodesic(true)
                                    .color(Color.BLACK)
                                    .add(new LatLng(prevLocation.getLatitude(), prevLocation.getLongitude()), locToAdd)
                                    .clickable(true)));

                            //m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();

                        } else {
                            m_aPL.add(mMap.addPolyline(new PolylineOptions().geodesic(true)
                                    .color(Color.RED)
                                    .add(new LatLng(prevLocation.getLatitude(),prevLocation.getLongitude()),locToAdd)
                                    .clickable(true)));
                            //m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
                        }
                    }
                    prevLocation.setLatitude(locToAdd.latitude);
                    prevLocation.setLongitude(locToAdd.longitude);

                }
            }

使用此代码,我可以获得所需的着色行为,但它并不平滑.我知道解决方案是一个折线选项,但是正如您将看到的那样,这并不能为我提供具有不同颜色的所需基本行为.这是放大的屏幕截图.因为我只是走路,所以很难看到锯齿状,但是通常情况下这是移动的车辆,因此差距更容易看到,例如

With this code I get the desired coloring behavior, but it's not smooth. I know the solution to this is one polylineoptions, but as you will see, that doesn't give me the desired basic behavior with the different colors. Here's a zoomed in screenshot. It's hard to see the jaggedness because I was just walking, but normally it would be for a moving vehicle so the gaps will be easier to see like here:

  1. 一个PolyLineOptions()方法:

  1. one PolyLineOptions() method:

for(HaulEvent event:cycle){ LatLng locToAdd = new LatLng(event.Latitude,event.Longitude);

for (HaulEvent event : cycle) { LatLng locToAdd = new LatLng(event.Latitude, event.Longitude);

                    if (event.cycle_num < 2) {

                        m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
                                .color(Color.BLACK)
                                .add(locToAdd)
                                .clickable(true)));
                        m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
                    } else { //after we've got a base cycle drawn
                        if (PolyUtil.isLocationOnPath(locToAdd, m_aBaseRoute, true, tolerance)) {
                            m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
                                    .color(Color.BLACK)
                                    .add(new LatLng(prevLocation.getLatitude(), prevLocation.getLongitude()), locToAdd)
                                    .clickable(true)));

                            m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();

                        } else {
                            m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
                                    .color(Color.RED)
                                    .add(new LatLng(prevLocation.getLatitude(),prevLocation.getLongitude()),locToAdd)
                                    .clickable(true)));
                            m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();
                        }
                    }
                    prevLocation.setLatitude(locToAdd.latitude);
                    prevLocation.setLongitude(locToAdd.longitude);

                }

m_PolyLines是我的PolyLineOptions.这样,当我没有偏离基本路线时,我得到:

m_PolyLines is my PolyLineOptions. With this way, when I haven't deviated from my base route I get:

然后,当我开始偏离基本路线时,我会得到一堆红色折线.每次我偏离时,红色都会一直返回到该特定点,在该点似乎没有什么特别的事情发生.

And then when I start to deviate from my base route I get a bunch of red polylines. Each time I deviated, the red would go all the way back to that specific point, where nothing special seemed to happen.

推荐答案

没有直接解决您的问题的方法,因为您只能控制折线的几个绘制参数,但是我提出了一种将两种方法混合使用的解决方法

There is no direct solution to your problem as you can only control a couple of drawing parameters for the polylines, but I propose a workaround that mixes both of your methods.

也就是说,将点添加到现有折线,直到必须更改颜色,然后再开始新的折线.这样,您只会在颜色改变时看到飞跃.

That is, addíng points to the existing polyline until it has to change the color, and then start a new polyline. This way you will only see a leap when the color changes.

这是示例代码(未经测试):

here is the example code (not tested):

int currentColor = Color.BLACK;

for (HaulEvent event : cycle) { LatLng locToAdd = new LatLng(event.Latitude, event.Longitude);
    if (event.cycle_num < 2) {
        m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
                .color(Color.BLACK)
                .add(locToAdd)
                .clickable(true)));
        m_aPolyLineList = (ArrayList<LatLng>) m_PolyLines.getPoints();

        currentColor = Color.BLACK;
    } else { //after we've got a base cycle drawn
        if (PolyUtil.isLocationOnPath(locToAdd, m_aBaseRoute, true, tolerance)) {
            if (currentColor == Color.BLACK) {
                m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
                    .color(Color.BLACK)
                    .add(new LatLng(prevLocation.getLatitude(), prevLocation.getLongitude()), locToAdd)
                    .clickable(true)));
            } else {
                m_aPL.add(mMap.addPolyline(new PolylineOptions().geodesic(true)
                    .color(Color.BLACK)
                    .add(new LatLng(prevLocation.getLatitude(), prevLocation.getLongitude()), locToAdd)
                    .clickable(true)));
            }

            currentColor = Color.BLACK;
        } else {
            if (currentColor == Color.RED) {
                m_aPL.add(mMap.addPolyline(m_PolyLines.geodesic(true)
                    .color(Color.RED)
                    .add(new LatLng(prevLocation.getLatitude(),prevLocation.getLongitude()),locToAdd)
                    .clickable(true)));
            } else {
                m_aPL.add(mMap.addPolyline(new PolylineOptions().geodesic(true)
                    .color(Color.RED)
                    .add(new LatLng(prevLocation.getLatitude(),prevLocation.getLongitude()),locToAdd)
                    .clickable(true)));
            }

            currentColor = Color.RED;
        }
    }
    prevLocation.setLatitude(locToAdd.latitude);
    prevLocation.setLongitude(locToAdd.longitude);
}

更新

使用 https://github.com/antoniocarlon/richmaps 进行改进(我是项目):

Improvement using https://github.com/antoniocarlon/richmaps (I am the owner of the project):

RichLayer richLayer = new RichLayer.Builder(mMap).zIndex(0).build();
RichPolylineOptions polylineOpts = new RichPolylineOptions(null)
    .zIndex(0)
    .strokeWidth(5)
    .strokeColor(Color.BLACK)
    .linearGradient(true);
RichPolyline polyline = polylineOpts.build();
richLayer.addShape(polyline);

// ...

for (HaulEvent event : cycle) { 
    LatLng locToAdd = new LatLng(event.Latitude, event.Longitude);

    if (event.cycle_num < 2) {
        polyline.add(new RichPoint(locToAdd));
    } else { //after we've got a base cycle drawn
        if (PolyUtil.isLocationOnPath(locToAdd, m_aBaseRoute, true, tolerance)) {
            polyline.add(new RichPoint(locToAdd));
        } else {
            polyline.add(new RichPoint(locToAdd).color(Color.RED));
        }
    }
}

这篇关于动态多段线颜色与Google Maps Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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