32

In my app I have some objects that have their location displayed on the map using markers. The problem is that the only way I've found to handle marker clicks is

googleMap.setOnMarkerClickListener(new ... {
    @Override
    public void onMarkerClick(Marker marker) {
       // how to get the object associated to marker???
    }
})

In other words I get the Marker object while the only interface that I have allows me to set just MarkerOptions.

Any way to associate Marker with an object?

3 Answers 3

54

I reckon this callback was not very thoroughly though by the Android team, but, it's what we have.

Whenever you call mMap.addMarker(); it returns the generated marker. You can then use a HashMap or some other data holder structure to remember it.

// Create the hash map on the beginning
WeakHashMap <Marker, Object> haspMap = new WeakHashMap <Marker, Object>();


// whenever adding your marker
Marker m = mMap.addMarker(new MarkerOptions().position(lat, lng).title("Hello World").icon(icon_bmp));
haspMap.put(m, your_data);
7
  • 8
    I recommend WeakHashMap, so when a Marker gets garbage-collected, so will its associated WeakHashMap entry and Object value. But, yes, unfortunately this seems to be the only option at present. Dec 27, 2012 at 12:25
  • 1
    makes sense to me. I edited my answer to be WeakHashMap. Is that ok?
    – Budius
    Dec 27, 2012 at 12:28
  • 5
    bzzzzt, wrong! I had to use an HashMap, otherwise the mapping gets somehow garbage collected.
    – dwery
    Aug 31, 2013 at 1:57
  • 11
    The documentation says that marker object may change, so don't use the marker as the key, use m.getId().
    – dwery
    Aug 31, 2013 at 2:05
  • hi @dwery , I can't find that on the documentation. Could you point to a link, please?
    – Budius
    Dec 18, 2013 at 10:50
36

You can associate arbitrary object by using Marker's setTag() method

Marker amarker = mMap.addMarker(new MarkerOptions().position(lat, lng).title("Hello World"));
amarker.setTag(new SomeData());

To retrieve data associated with marker, you simply read it using its getTag() and then cast it to its original type.

SomeData adata = (SomeData) amarker.getTag();

More information

4
  • 1
    This is the correct solution. As per documentation about Tag in Marker : "An Object associated with the marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map<Marker, Object>. "
    – JDenais
    Sep 30, 2016 at 9:10
  • Correct solution as of Play Services 9.4
    – hibob
    Oct 11, 2016 at 9:34
  • This is the RIGHT solution! Nov 15, 2016 at 11:31
  • That's great that after 4 years they've implemented this out-of-the-box =)
    – leshka
    Nov 29, 2016 at 18:42
8

Another option would be to create a Map whose keys is marker.getId() and the value is our object.

In this way, we wouldn't keep a reference to a Marker in memory, and wouldn't have to worry about garbage collected markers.

Here you can see more answers.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.