简体   繁体   English

如何使用条件添加和删除折线

[英]How add and remove polylines with conditionals

I try to build to app, that it show a route with polylines, I need to add and remove polylines with conditionals, from a String that is equal to inicioR for show, and finR for remove 我尝试建立到应用程序,它显示出与折线的路线,我需要添加和用条件删除折线,从一个字符串等于inicioR作秀,并finR为删除

Below code fragment of my activity.java . 下面是我的activity.java代码片段。 I try to do it like 我尝试这样做

@Override
public void onDirectionFinderSuccess(List<Route> routes) {
    progressDialog.dismiss();
    polylinePaths = new ArrayList<>();
    originMarkers = new ArrayList<>();
    waypoints = new ArrayList<>();
    destinationMarkers = new ArrayList<>();

    for (Route route : routes) {
        //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 16));
        ((TextView) findViewById(R.id.tvDuration)).setText(route.duration + " Min");
        ((TextView) findViewById(R.id.tvDistance)).setText(route.distance + " Kms");


        PolylineOptions polylineOptions = new PolylineOptions().
                geodesic(true).
                color(Color.BLUE).
                width(10);

        for (int i = 0; i < route.points.size(); i++)
            polylineOptions.add(route.points.get(i));



        if (estado == "inicioR") {
            polylinePaths.add(mMap.addPolyline(polylineOptions));
        }else if (estado == "finR"){
            polylinePaths.remove(mMap.addPolyline(polylineOptions));
        }

    }
}

I haven't run this, but it should work 我还没有运行,但是应该可以

if (estado == "inicioR") {
    polylinePaths.add(mMap.addPolyline(polylineOptions));
}else if (estado == "finR"){
    if (polylinePaths != null && !polylinePaths.isEmpty()) {
        polylinePaths.get(polylinePaths.size()-1).remove(); // remove the lastest line you added. 
        polylinePaths.remove(polylinePaths.size()-1); // remove the lastest line record from ArrayList.
    }
}

To remove the Polyline, you can use the Polyline.remove() method. 要删除折线,可以使用Polyline.remove()方法。

In your case, you need to do the correct check for String equality with String.equals() method. 在您的情况下,您需要使用String.equals()方法对字符串是否相等进行正确的检查。 Using == is logically incorrect because it will test for reference of the String. 从逻辑上讲,使用==是不正确的,因为它将测试对String的引用。 Read more at How do I compare strings in Java? 在“ 如何比较Java中的字符串”中阅读更多内容

Hence, your code should be something like this: 因此,您的代码应如下所示:

if (estado.equals("inicioR")) {
   ..
} else if (estado.equals("finR")){
   ...
}

After that, to remove the Polyline you need to remove the polyline object first then remove the polyline from the list. 之后,要删除折线,您需要先删除折线对象,然后再从列表中删除折线。

if (estado.equals("inicioR")) {
   polylinePaths.add(mMap.addPolyline(polylineOptions));
} else if (estado.equals("finR")){
   // Get the polyline that you want to remove from the list
   // by the index. Here for the example the index is 1
   int index = 1;
   Polyline polyline = polylinePaths.get(index);
   // you need to check for IndexOutOfBoundsException.
   // In this example code, assume that IndexOutOfBoundsException never happened.

  // remove the polyline from the map.
  polyline.remove();
  // then remove from the list.
  polylinePaths.remove(index);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM