【问题标题】:Remove the last plotted line from Google Map Android从 Google Map Android 中删除最后绘制的线
【发布时间】:2015-11-08 13:23:06
【问题描述】:

我有以下代码将 PolylineOptions 对象添加到折线数组列表中。传入的参数是从查询返回到 Directions API 的一系列点。

List<Polyline> polylines = new ArrayList<Polyline>();

public void drawPolylines(List<List<HashMap<String, String>>> result) {
    PolylineOptions lineOptions = null;
    // Traversing through all the routes
    for (int i = 0; i < result.size(); i++) {
        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);
            double lat = Double.parseDouble(point.get("lat"));
            double lng = Double.parseDouble(point.get("lng"));
            LatLng position = new LatLng(lat, lng);
            lineOptions.add(position).width(6).color(Color.BLUE);
        }
        polylines.add(mMap.addPolyline(lineOptions));
    }
}

当我单击撤消按钮时,我希望它从地图中删除最后一行,即折线列表中的最后一行。我有以下代码,但每当我运行它时,似乎都没有从地图中删除。还有什么需要补充的吗?

    mButtonUndo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // create variable for the 2nd last point clicked and assign value form markerPoints array list
            LatLng lastPoint;
            lastPoint = markerPoints.get(markerPoints.size() - 2);

            // animate camera to centre on the previously touched position
            System.out.println("Centering camera to previous position at " + lastPoint.toString());
            mMap.animateCamera(CameraUpdateFactory.newLatLng(lastPoint));

            // remove line from the map
            for (int i = 0; i == polylines.size(); i++) {
                polylines.remove(polylines.size());
            }

            // remove value from the markerPoints array list

            // update the distance text
            double routeDistance = 0;
            distanceCount.remove(distanceCount.size() - 1);
            for (Double step : distanceCount) {
                routeDistance += step;
            }

            System.out.println("Total Distance calculated in undo in m = " + routeDistance);
            // output new value to ui
            mDistanceCount.setText(routeDistance / 1000 + "km");
        }
    });

【问题讨论】:

    标签: java android google-maps android-studio google-maps-android-api-2


    【解决方案1】:

    通过在增强的 for 循环中使用简单检查找到了解决方案:

        mButtonUndo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                // create variable for the 2nd last point clicked and assign value form markerPoints array list
                LatLng lastPoint;
                lastPoint = markerPoints.get(markerPoints.size() - 2);
    
                // animate camera to centre on the previously touched position
                System.out.println("Centering camera to previous position at " + lastPoint.toString());
                mMap.animateCamera(CameraUpdateFactory.newLatLng(lastPoint));
    
    
                // remove polyline object from the map
                for (Polyline line : polylines) {
                    if (polylines.get(polylines.size() - 1).equals(line)) {
                        line.remove();
                        polylines.remove(line);
                    }
                }
    
                // remove last value from the markerPoints array list
                markerPoints.remove(markerPoints.size() - 1);
    
                // update the distance text
                double routeDistance = 0;
                distanceCount.remove(distanceCount.size() - 1);
                for (Double step : distanceCount) {
                    routeDistance += step;
                }
    
                System.out.println("Total Distance calculated in undo in m = " + routeDistance);
                // output new value to ui
                mDistanceCount.setText(routeDistance / 1000 + "km");
            }
        });
    

    【讨论】:

      【解决方案2】:

      您可以使用清除地图 地图.clear();

      【讨论】:

      • 这没有帮助。
      猜你喜欢
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 2011-08-04
      • 2012-12-26
      • 2014-10-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多