dynamic Polyline colors with google maps android app

后端 未结 2 1089
感情败类
感情败类 2021-01-26 22:37

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 initi

2条回答
  •  Happy的楠姐
    2021-01-26 22:47

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

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

提交回复
热议问题