简体   繁体   中英

How to Remove multiple markers from map using MapBox SDK

I am using mapbox sdk for offline map, i have added markers like

public void parseStops(String type) {

    if (type.equals(activity.getResources().getString(R.string.allStops))) {
        alStops = stopsOperations.selectData("walking");
    } else {
        Log.d("RouteType:", ":" + type);
        alStops = stopsOperations.selectDataByType(type);
    }

    if (alStops.size() > 0) {
        for (int i = 0; i < alStops.size(); i++) {
            stopsEntity = new StopsEntity();
            stopsEntity = alStops.get(i);
            title = stopsEntity.getTitle();
            lonngt = stopsEntity.getLongitude();
            lat = stopsEntity.getLattitude();
            routeType = stopsEntity.getType();
            stopNumber = stopsEntity.getStopNumber();

            Log.d("stopNumber:", ":" + stopNumber);

            marker = new Marker(stopsEntity.getTitle(), "", new LatLng(
                    Double.parseDouble(stopsEntity.getLattitude()),
                    Double.parseDouble(stopsEntity.getLongitude())));
            View view = ((LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(R.layout.custom_marker_layout, null);
            ivPin = (ImageView) view.findViewById(R.id.ivPin);
            tvStopNumber = (TextView) view.findViewById(R.id.tvStopNumber);
            if (routeType != null) {

                if (routeType.equals(activity.getResources().getString(
                        R.string.walking1))
                        && ((alStops.get(i).getStopNumber().equals("79"))
                                || (alStops.get(i).getStopNumber()
                                        .equals("98"))
                                || (alStops.get(i).getStopNumber()
                                        .equals("99")) || (alStops.get(i)
                                .getStopNumber().equals("125")))) {
                    ivPin.setImageResource(R.drawable.walking);
                } else if (routeType.equals(activity.getResources()
                        .getString(R.string.red))) {
                    ivPin.setImageResource(R.drawable.pin_red);
                } else if (routeType.equals(activity.getResources()
                        .getString(R.string.blue))) {
                    ivPin.setImageResource(R.drawable.blue_pin);

                } else if (routeType.equals(activity.getResources()
                        .getString(R.string.orange))) {
                    ivPin.setImageResource(R.drawable.pin_orange);

                } else if (routeType.equals(activity.getResources()
                        .getString(R.string.yellow))) {
                    ivPin.setImageResource(R.drawable.pin_yellow);

                } else if (routeType.equals(activity.getResources()
                        .getString(R.string.lilac))) {
                    ivPin.setImageResource(R.drawable.pin_lilac);

                } else if (routeType.equals(activity.getResources()
                        .getString(R.string.interchangee))) {
                    ivPin.setImageResource(R.drawable.pin_interchange);

                }
            }
            if (stopNumber != null) {
                if (stopNumber.length() < 2) {
                    tvStopNumber.setText("0" + stopNumber.toString());
                } else {
                    tvStopNumber.setText(stopNumber.toString());
                }
            }
            if (routeType != null
                    && (alStops.get(i).getTitle().equalsIgnoreCase(activity
                            .getResources().getString(R.string.start)))
                    || (alStops.get(i).getTitle().equalsIgnoreCase(activity
                            .getResources().getString(R.string.end)))) {
                marker.setMarker(new BitmapDrawable(UDF
                        .createDrawableFromView1(activity, view)));
            } else {
                marker.setMarker(new BitmapDrawable(UDF
                        .createDrawableFromView1(activity, view)));
            }
            mv.addMarker(marker);
            if (routeType != null
                    && !routeType.equals(activity.getResources().getString(
                            R.string.walking1))) {
                marker = new Marker(stopsEntity.getTitle(), "", new LatLng(
                        Double.parseDouble(stopsEntity.getLattitude()),
                        Double.parseDouble(stopsEntity.getLongitude())));
                marker.setMarker(activity.getResources().getDrawable(
                        R.drawable.bus));
                mv.addMarker(marker);

            }
        }

    }
}

After that i need to remove these all added markers for some cases. So, how can i remove all the markers simultaneously. Can any one help me?

The MapView class has a method called clear :

void clear() Remove all markers from the map's display.

https://www.mapbox.com/mapbox-android-sdk/api/0.7.0/com/mapbox/mapboxsdk/views/MapView.html#clear()

For mapbox-android-sdk:3.0.0, the method is:

mMapView.removeAllAnnotations();

And, the .clear() method is no more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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