首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不能移除Polyline

我不能移除Polyline
EN

Stack Overflow用户
提问于 2018-02-14 14:28:10
回答 1查看 306关注 0票数 0

我构建了一个应用程序来用另一个点跟踪我的当前位置,当我的当前位置被更新时,创建一个新的polyline,但是最后一个polyline没有删除。

我试着用polyline.remove();,但不工作。

这是我使用的代码片段:

代码语言:javascript
复制
 protected void onPostExecute(List<List<HashMap<String, String>>> result) {
            ArrayList<LatLng> points = null;
            PolylineOptions lineOptions = null;
            MarkerOptions markerOptions = new MarkerOptions();
            String distance = "";
            String duration = "";

            if (result.size() < 1) {
                Toast.makeText(getBaseContext(), "No Points", Toast.LENGTH_SHORT).show();
                return;
            }

            // 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");
                        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(4);
                lineOptions.color(Color.BLUE);

            }

            tvDistanceDuration.setText("Distance:" + distance + ", Duration:" + duration);

             polyline = mMap.addPolyline(lineOptions);

        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-14 14:35:32

您需要通过使用列表来跟踪您的折线。然后,如果要删除polyline,可以将其从列表中删除。就像这样:

代码语言:javascript
复制
List<Polyline> polylines = new ArrayList<>();

// When adding the polyline add to the list
polylines.add(mMap.addPolyline(lineOptions));

// To remove all the polyline
for(Polyline polyline: polylines) {
  polyline.remove();
}

// then clear the list
polylines.clear();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48789565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档