hiveship
hiveship

Reputation: 308

Android Google Map change polyline colors

I am drawing polylines on my Google Map. I'm doing it using:

private Map<UUID, PolylineOptions> data;

private void drawFeatures() {
    for (Feature feature : features) {
        feature.setUuid(UUID.fromString((String) feature.getProperties().get("id")));

        PolylineOptions options = new PolylineOptions();

        List<Coordinates> coordinates = ((LineString) feature.getGeometry()).getCoordinates();
        for (Coordinates coordinate : coordinates) {
            // can't use "addAll(...) since 'coordinates' are not instance of 'LatLng'
            options.add(new LatLng(coordinate.getLatitude(), coordinate.getLongitude()));
            options.color(Color.RED);
        }
        mMap.addPolyline(options);
        data.put(feature.getUuid(), options);
    }
}

And then everything is OK. All my polylines are correctly drawed using the good width and color.

However, after that, I'm trying to update the width and the color (without removing and redrawing all the polylines). I'm trying to do it with:

   private void changeColor() {
        for (Map.Entry<UUID, PolylineOptions> entry : data.entrySet()) {
            entry.getValue().color(Color.CYAN);
        }
    }

But there is no changes on my map :/ I've read the Google Developers documentation and I don't find anything about that.

How can I update the color of a polyline without having to remove and re-add it ?

Upvotes: 1

Views: 2270

Answers (2)

Mehul Gajjar
Mehul Gajjar

Reputation: 431

may this help you

MarkerOptions markerOptions = new MarkerOptions();
        if (!status.equals("ZERO_RESULTS")) {
            for (int i = 0; i < result.size(); i++) {
                points = new ArrayList<LatLng>();
                points.add(driverLatLng);
                lineOptions = new PolylineOptions();
                List<HashMap<String, String>> path = result.get(i);
                for (int j = 0; j < path.size(); j++) {
                    HashMap<String, String> point = path.get(j);

                    double lat = Double.parseDouble(point.get("lat"));
                    double lng = Double.parseDouble(point.get("lng"));
                    LatLng position = new LatLng(lat, lng);
                    points.add(position);
                }
                points.add(storeLatLng);
                if (points.contains(storeLongitude)) {
                    int index = points.indexOf(storeLongitude);
                    AppLog.Log(TAG, "Index Value in " + index);
                    int length = points.size();
                    points.subList(index, length - 1);
                    AppLog.Log(TAG, "Arraylist Size after SubList Array" + points.size());
                }
                lineOptions.addAll(points);
                lineOptions.width(13);
                lineOptions.color(getResources().getColor(R.color.title_background));
                lineOptions.geodesic(true);
                lineOptions.zIndex(100);
                AppLog.Log(TAG, "lineOptions is" + lineOptions.getPoints());
            }
            mMap.addPolyline(lineOptions);
            //DrawArrowHead(mMap, driverLatLng, storeLatLng);
        } else {

            GlobalMethod.snackBar(true,activity_driver,appContext,"no root found.");

        }

Upvotes: 0

antonio
antonio

Reputation: 18242

PolylineOptions is just a builder for Polylines which are the objects that are drawn into the map.

Thus, changing the PolylineOptions will not affect the Polylines once they are on the map.

Your private Map<UUID, PolylineOptions> data; should be private Map<UUID, Polyline> data; and you need to add elements to the Map like this:

data.put(feature.getUuid(), mMap.addPolyline(options));

Upvotes: 2

Related Questions