Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 561
bofredo Avatar asked Aug 08 '13 12:08

bofredo


People also ask

How do I get a marker position on Google Maps?

You can add a simple marker to the map at a desired location by instantiating the marker class and specifying the position to be marked using latlng, as shown below.

What is the marker on Google Maps called?

The Google Maps pin is the inverted-drop-shaped icon that marks locations in Google Maps. The pin is protected under a U.S. design patent as "teardrop-shaped marker icon including a shadow".


1 Answers

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!
}
like image 76
bofredo Avatar answered Sep 21 '22 06:09

bofredo