Android Google地图更改折线颜色

时间:2017-01-19 09:57:16

标签: java android google-maps

我正在Google地图上绘制折线。我正在使用:

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

然后一切都好。我的所有折线都使用良好的宽度和颜色正确绘制。

然而,在那之后,我正在尝试更新宽度和颜色(不删除和重绘所有折线)。 我正试着这样做:

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

但我的地图上没有任何变化:/我已阅读Google开发者文档,但我找不到任何相关内容。

如何在不必删除和重新添加折线的情况下更新折线的颜色?

2 个答案:

答案 0 :(得分:2)

PolylineOptions只是Polylines的构建器,它是绘制到地图中的对象。

因此,更改PolylineOptions一旦在地图上,就不会影响Polyline

您的private Map<UUID, PolylineOptions> data;应为private Map<UUID, Polyline> data;,您需要像Map这样添加元素:

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

答案 1 :(得分:0)

可以帮助您

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

        }
相关问题