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

3

6

i am currently trying to implement an ActionBar-Button that on usage sets all my markers on my GoogleMap-object visible or invisible. My problem is that i don't know how i can get a reference to all my markers once they have been created and are shown on my map. Im looking for a solution where i stash all my marker-objects into an array, that i can access in other parts of my code aswell. is this approach reasonable?

here is what i am thinking of:

 private Marker[] mMarkerArray = null;
 for (int i = 0; i < MainActivity.customers.size(); i++) {

     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);                      
   }

and set all my markers invisible on within another method:

for (int i = 0;  i < mMarkerArray.length;; i++) {
    mMarkerArray[i].setVisible(false);
}

it refuses to add the markers to a Marker[]-array. how can i achieve it?

mMarkerArray.add(marker) doesnt work

While answered 8/8, 2013 at 12:45 Comment(0)
W
17

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!
}
While answered 8/8, 2013 at 14:40 Comment(0)
W
1

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.

Wesla answered 8/8, 2013 at 12:59 Comment(3)
i thought this was just valid for maps v1. im currently trying to port an app from v1 to v2.While
oh, sorry about that, my android mapview app is still v1, I didn't realize this changed.Wesla
np, btw my v1-version is doing it the OverlayItem-way. but i dont figure how to adapt it for v2 as there is no MapView anymore.While
M
1

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.

Molnar answered 8/8, 2013 at 14:29 Comment(3)
thx. my major-problem was that i didnt figure that there were data-sets in the database that had coordinates with 0,0 values and that my HashMAp-attempt tried to get the wrong markers (not the ones WITH coordinates)While
This seems reasonable and looks the most promising, but I can't find the getMarkers() method (I'm using Kotlin, which seems to hide getters sometimes??). Any suggestions?Verenaverene
googleMap doesn't have getMarkers()Charentemaritime

© 2022 - 2024 — McMap. All rights reserved.