Associate an object with Marker (google map v2)
Asked Answered
C

3

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?

Courtenay answered 27/12, 2012 at 11:43 Comment(0)
D
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

Devy answered 24/8, 2016 at 11:51 Comment(4)
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>. "Shockey
Correct solution as of Play Services 9.4Countersubject
This is the RIGHT solution!Nectarine
That's great that after 4 years they've implemented this out-of-the-box =)Courtenay
D
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);
Dubuffet answered 27/12, 2012 at 12:3 Comment(7)
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.Gerhardt
makes sense to me. I edited my answer to be WeakHashMap. Is that ok?Dubuffet
bzzzzt, wrong! I had to use an HashMap, otherwise the mapping gets somehow garbage collected.Stokehold
The documentation says that marker object may change, so don't use the marker as the key, use m.getId().Stokehold
hi @Stokehold , I can't find that on the documentation. Could you point to a link, please?Dubuffet
@Stokehold If you do so, you have no chance to remove the marker because you don't have the reference.Haigh
@Gerhardt unless you use this WeakHashMap throughout the app it is not useful for this purpose.Lepage
D
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

Devy answered 24/8, 2016 at 11:51 Comment(4)
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>. "Shockey
Correct solution as of Play Services 9.4Countersubject
This is the RIGHT solution!Nectarine
That's great that after 4 years they've implemented this out-of-the-box =)Courtenay
M
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.

Minute answered 21/11, 2013 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.