How can i access all marker on my GoogleMap-Object (android maps v2) and set them (in)visible?

流过昼夜 提交于 2019-12-03 04:00:45

i figured out an answer that also regards my customerList having customers without coordinates --> (0,0;0,0). inspired by this blog.

initialize ArrayList:

private ArrayList<Marker> mMarkerArray = new ArrayList<Marker>();

add marker to my map and to the mMarkerArray:

for (int i = 0; i < MainActivity.customers.size(); i++) {
        Customer customer = MainActivity.customers.get(i);
        if (customer.getLon() != 0.0) {
            if (!customer.isProspect()) {
                Data mData= new Data(customer.getLat(),customer.getLon(),customer.getName(),
                        customer.getOrt());

                LatLng location = new LatLng(mData.lat, mData.lng);

                Marker marker = mMap.addMarker(new MarkerOptions().position(location)
                          .title(mData.title)
                          .snippet(mData.snippet));

                mMarkerArray.add(marker); 
}}} 

set all markers not-visible

for (Marker marker : mMarkerArray) {
    marker.setVisible(false);
    //marker.remove(); <-- works too!
}

You can keep a Collection of OverlayItem within your Activity or Fragment and then call MapView.getOverlays().clear() to make them "invisible" and then add them back to make them visible again. Call MapView.invalidate() after each action to cause the map to be repainted.

Replace

private Marker[] mMarkerArray = null;

with

private List<Marker> mMarkerArray = new ArrayList<Marker>();

and you should be fine.

If you use Android Maps Extensions, you can simply iterate over all markers using

for (Marker marker : googleMap.getMarkers()) {
    marker.setVisible(false);
}

without having your own List of all Markers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!