mapbox-gl-native icon indicating copy to clipboard operation
mapbox-gl-native copied to clipboard

Add custom info to identify a marker in Android

Open danigonzalez opened this issue 11 years ago • 4 comments

Hi, I would like to put some sort of object or id to identify a marker when it's touched. Is this possible?

I've been looking at the source code and I think there's nothing I can do, since MarkerOptions is a final class so I can't extend it to add my identifier info, and with Marker or Annotation I should not use setId method to set my custom id. and I can only add a MarkerOptions to a MapView, so I can't make my custom Annotation class.

My application markers represent different things, so when a marker is touched, I need to be able to identify it.

Thanks a lot!

danigonzalez avatar Nov 16 '15 12:11 danigonzalez

I also ran into this limitation. My current workaround is to maintain my own dictionary mapping the auto-generated annotation id to my user-defined marker ID.

For parity, note that a Mapbox GL JS marker implemented as a GeoJSON feature can have a user-defined ID. The published GeoJSON marker example doesn't take advantage of this, but assuming the GeoJSON spec is fully supported, it should at least accept them.

Unfortunately, in the JS SDK I haven't found any way to detect when a marker is touched, and identify it, other than specifying the url property in the GeoJSON (which makes the marker a link) and then watching for a page navigation event. In this case, the url becomes the id.

Anyway, for the native SDKs I agree with the @danigonzalez that it would be really helpful to have user-defined marker ids, and to have those ids included when we are notified of user interaction with specific markers.

bobmeyers5 avatar Dec 03 '15 17:12 bobmeyers5

To quote @incanus answer on SO on an iOS related question:

You want to subclass the annotation class to add your own properties

We currently don't allow subclassing on Android, I'm going to see if we can remove this limitation.

tobrun avatar Jan 15 '16 08:01 tobrun

I have been looking into this and it won't be the easy fix of removing final keyword from class. This is because we have a builder pattern setup with MarkerOptions which builds our Marker object. We should look into if applying generics, while still enforcing inheritance of Marker, will solve this problem:

Something like:

   MarkerOptions<? extends Marker>

This would mean we will not have API parity with Google Maps SDK.

This is not something we should fix with 3.1.0 but we should try tackling this with 4.0.0

tobrun avatar Jan 15 '16 10:01 tobrun

We have experimental subclasses in place after #3756 landed. You should be able to add custom information to it. Closing this issue for now.

tobrun avatar Feb 17 '16 07:02 tobrun