1

I'm adding dynamically a non-fixed amount of markers in a map, which each of them are related to one instance of my POCO class.

I need to link them so when the user clicks on one of the markers, I show the rest of the data inside the custom InfoWindow.

What do you suggest?

PS: I add new markers everytime the user pans or zooms the map and I worried about overloading the app. Are the non visible markers disposed?

juliano.net
  • 7,982
  • 13
  • 70
  • 164

2 Answers2

5

I suggest using a HashMap or something similar. As you iterate over your list of objects and create markers for them, also add the Marker to a list, using the ID of the object as the key, and the marker as the value:

private HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();

...

for(MarkerObject obj : this.markerObjects)
{
     //If the marker isn't already being displayed
     if(!markerMap.containsKey(obj.getId()))
     {
         //Add the Marker to the Map and keep track of it 
         this.markerMap.put(obj.getId(), this.mMap.addMarker(getMarkerForObject(obj)));
     }
}

Then you can use a OnInfoWindowClickListener to find the object id of the tapped marker in your Map and do something with the corresponding data, like open a new activity with details.

DiscDev
  • 38,652
  • 20
  • 117
  • 133
  • Hi, this question is related to the other (http://stackoverflow.com/questions/14318258/how-google-maps-android-api-v2-handle-markers-outside-the-view-are/14323819#14323819) that you also answered. I'm worried about how to control the number of items on the HashMap and I'd like to remove the items that are not in the visible area. What do you suggest? – juliano.net Jan 14 '13 at 18:13
  • 1
    I suggest doing something similar to what I wrote up in this blog post: http://discgolfsoftware.wordpress.com/2012/12/06/hiding-and-showing-on-screen-markers-with-google-maps-android-api-v2/ – DiscDev Jan 14 '13 at 18:18
  • 1
    Doesn't this disregard the warning on the [MapFragment documentation](https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment) page about holding on to GoogleMap objects? – WeNeigh May 31 '13 at 11:38
  • @WeNeigh: See http://stackoverflow.com/questions/16899612/android-maps-markers-and-memory-leaks – skaffman Jul 03 '13 at 12:45
0

I know this post is old, but if you are using the prefab map Activity in Android studio

In the set up map

  private void setUpMap() {


    Map<String,someObject>markerInfoList = new HashMap<String,someObject>();

  // get the marker Id as String
       String id =  mMap.addMarker(new MarkerOptions().position(new LatLng(/*set Latitude*/,  /*setLongitude*/).title("Marker")).getId();
       //add the marker ID to Map this way you are not holding on to  GoogleMap object
        markerInfoList.put(id,mapppedHouses.get(i));     
}

Then in the :

  private void setUpMapIfNeeded() {
  ///...
 if (mMap != null) {
   //if a marker is clicked
   mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    someObject = markerInfoList.get(marker.getId());
                }
            });
  }
 }
Tiber
  • 116
  • 1
  • 18