首页 文章

动态折线颜色与谷歌 Map Android应用程序

提问于
浏览
1

我正在创建一个使用谷歌 Map 的应用程序,以显示您偏离之前采用的路径 . 我可以用不同的颜色显示偏差的位置,但是我必须初始化一个新的PolyOptions(),导致“波涛汹涌”的折线而不是像我的基本路线那样的平滑折线 . 如果我没有初始化一个新的PolyOptions(),那么当我开始偏离时,它会将路径的很大一部分变为红色,即使我只是尝试将颜色应用于那个特定的折线 . 当我回到路上时,在偏离之后,我失去了偏离的颜色,这一切都回到了黑色 . 任何人都可以向我解释发生了什么,以及如何将新PolyOptions()方法中的正确着色与使用一个PolyLineOptions()获得的平滑度相结合

  • new PolyLineOptions()方法:

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

                }
            }

使用此代码,我得到了所需的着色行为,但是它给了我不同颜色所需的基本行为 . 在这里很难看到锯齿状,因为我只是在行走,但通常它会是一辆移动的车辆所以间隙会更容易看到here

enter image description here

  • 一个PolyLineOptions()方法:

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 . 通过这种方式,当我没有偏离我的基本路线时,我得到:
enter image description here

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

2 回答

  • 0

    你必须为addPolyline()提供一个Polylineoptions到googlemaps对象 . 并且要更改PolylineOptions的颜色,您可以使用color() . 或者要动态更改googlemaps中折线的颜色,您可以将线对象保存在折线中,然后使用setColor()在代码的任何位置更改折线的颜色,只要折线对象不为空即可 .

    要更改在谷歌 Map 上绘制的任何折线的颜色或要绘制,您可以为每个折线设置一种方法 .

    第一个案例(polylineoptions

    polylineoptions是您传递给addpolyline()以绘制到googlemaps的对象 . 所以要在这种情况下改变颜色,你必须在分配之前改变颜色 . 使用color()方法 . 但问题是,一旦你改变了颜色并分配了你就无法再改变它 . 要再次更改它,您必须清除谷歌 Map 并重绘折线 .

    第二种情况(polyline

    当您通过addpolyline()polylineoptions分配给googlemaps时,您可以将此对象保存到polyline . 然后,您可以在googlemaps上绘制后,在任何时间点更改此折线的颜色 . 如果要在绘制折线后动态更改颜色,这将很容易 . 您将使用的方法是setColor() .

    我希望这能解决你的疑问 .

    谢谢

  • 0

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

    也就是说,addíng指向现有的折线,直到它必须改变颜色,然后开始一个新的折线 . 这样,只有在颜色发生变化时才会看到跳跃 .

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

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

    Update

    使用https://github.com/antoniocarlon/richmaps改进(我是项目的所有者):

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

相关问题