【问题标题】:Android Remove Polyline or replaceAndroid 删除折线或替换
【发布时间】:2017-02-02 11:26:08
【问题描述】:

在我的应用程序中,我在地图上添加了 2 个标记。当我通过单击添加第二个标记时,我调用一个函数来计算路线并在地图上添加折线。 这是折线的代码

protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;
        MarkerOptions markerOptions = new MarkerOptions();
        String distance = "";
        String duration = "";
        String value_duration = "";


        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) {
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);


                if (j == 0) {    // Get distance from the list
                    distance = (String) point.get("distance");
                    continue;
                } else if (j == 1) { // Get duration from the list
                    duration = (String) point.get("duration");
                    value_duration = (String) point.get("duration_");
                    continue;
                }

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                points.add(position);
            }

            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            lineOptions.width(10);
            lineOptions.color(Color.parseColor("#F7B907"));

        }

        // Drawing polyline in the Google Map for the i-th route
        distanza_percorso = distance;
        durata_percorso = duration;

        testo_car.setText(durata_percorso);

        durata_tragitto = Float.parseFloat(value_duration);
        Log.d("durata_tragitto", "durata: " + value_duration);

        polylineFinal = map.addPolyline(lineOptions);


        moveToBounds(map.addPolyline(lineOptions));


    }

现在,如果我替换第二个标记并调用新的计算路线,我想通过地图删除折线。 我已经写了这个代码:

if (polylineFinal != null){
            polylineFinal.remove();
        }

但地图上的折线不会被移除。 你能帮我吗?知道为什么吗?

谢谢

【问题讨论】:

    标签: android google-maps polyline


    【解决方案1】:

    不确定,但请尝试使用此更新添加折线代码,

    polylineFinal = map.addPolyline(lineOptions);
    moveToBounds(polylineFinal);
    

    【讨论】:

      猜你喜欢
      • 2016-07-15
      • 2021-01-13
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 2019-03-08
      相关资源
      最近更新 更多