首页 文章

Android Google Map 更改折线颜色

提问于
浏览
1

我正在Google Map 上绘制折线 . 我正在使用:

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

但是我的 Map 上没有任何变化:/我已经阅读了Google Developers文档,但我没有找到任何相关信息 .

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

2 回答

  • 0

    PolylineOptions 只是 Polylines 的构建器,它是绘制到 Map 中的对象 .

    因此,一旦它们在 Map 上,更改 PolylineOptions 将不会影响 Polyline .

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

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

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

相关问题