18

When using Google Map API V3 I can add custom property in the following way:

var marker = new google.maps.Marker({
  position: userLocation,
  map: myGoogleMap,
  animation: google.maps.Animation.DROP,
  icon: avatar,
  title: userName,
  customProperty1: bla,
  customProperty2: bla,
  customProperty3: bla,
  ...

});

I'm wondering if I can do the same for API V2 Android, the reason I want to do this is that each info window of each marker need to know some information of that marker. And I'm trying to achieve this in render function below:

private void render(Marker marker, View view) {
        int badge = R.drawable.android_face;

        if (marker.customProperty)
        //here I need to know this property to decide which image to use        
                         badge = R.drawable.bla;

        ((ImageView) view.findViewById(R.id.badge))
                    .setImageResource(badge);

}
Arch1tect
  • 4,128
  • 10
  • 48
  • 69

2 Answers2

47

You cannot directly extend Marker, because it is a final class, but you have some options:

0) As of Google Maps Android API v2 version 9.4.0, you can use Marker::getTag and Marker::setTag. This is most likely the preferred option.

1) Create a map to store all additional information:

private Map<Marker, MyData> allMarkersMap = new HashMap<Marker, MyData>();

When creating a marker, add it to this map with your data:

Marker marker = map.addMarker(...);
allMarkersMap.put(marker, myDataObj);

Later in your render function:

MyData myDataObj = allMarkersMap.get(marker);
if (myDataObj.customProp) { ...

2) Another way would be to use Marker.snippet to store all the info as a String and later parse it, but that's kinda ugly and unmaintainable solution.

3) Switch from plain Google Maps Android API v2 to Android Maps Extensions.

This is very similar to point 1, but you can directly store MyData into marker, using

marker.setData(myDataObj);

and later:

MyData myDataObj = (MyData) marker.getData();
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • 1
    thx~ I chose the first method in the end and it works. Any idea why v2 doesn't allow extending? just curious.. – Arch1tect Jun 08 '13 at 23:47
  • @Arch1tect Design choice based on the decision to use separate process to do real work. Objects you interact with are nothing more than proxies. – MaciejGórski Jun 09 '13 at 00:24
  • @MaciejGórski Pls. check my question how can i get PropertyID at percular pin click [http://stackoverflow.com/questions/20784094/get-custom-class-data-information-on-map-v2-popupclick-event-in-android](http://stackoverflow.com/questions/20784094/get-custom-class-data-information-on-map-v2-popupclick-event-in-android) – Harshal Kalavadiya Mar 01 '14 at 05:34
  • @MaciejGórski Hi. I've tried the map approach, when onClick of a marker is called, I want to use the MarkerOptions-object that has been passed to the onClickFunction() to retrieve my custom object from the map. But it always return null. Any ideas? – Androidicus Mar 27 '15 at 10:39
  • 1
    The problem with `allMarkersMap` is that it will be re-initialized to empty map whenever the activity is re-created (consider e.g. display rotation). The class `Marker` is not serializable, i.e. the map cannot be stored to a bundle directly. – Cimlman Mar 10 '16 at 15:41
  • I save a json serialized object in the Marker.snippet. Ok it's ugly, but not so unmaintainable. IMHO the best is the third option. :) – Evilripper Jan 23 '17 at 11:32
  • 1
    @Evilripper I also like 3rd option, but there is 4th right now. Google Maps API v2 developers added `Marker::getTag` and `Marker::setTag` recently. Very similar to `getData` / `setData` in AME. – MaciejGórski Jan 23 '17 at 13:40
  • @MaciejGórski Ah nice, thanks for information. In fact some time ago I searched Tag property but I didn't found it. In many languages there is always a way to set an object into an item of a list. :) You should add the 4th in the answer. – Evilripper Jan 23 '17 at 14:16
  • 1
    it works! from version com.google.android.gms:play-services:9.4.0 Marker marker=mMap.addmarker(new MarkerOptions(...)); [...] public boolean onMarkerClick(Marker arg0) { arg0.getTag(); – Evilripper Jan 23 '17 at 15:23
2

I used Marker.setTag(Object o) and .getTag() to pass my custom location obj. to the Marker. Of course the Object can be any kind of object you want.

Marker m = googleMap.addMarker(new MarkerOptions()
                .position( ... )
                // setTag is not part of MarkerOptions so can't be defined here
              );
m.setTag(location); // Marker.setTag()

And accessing that obj. in InfoWindowAdapter is easy:

@Override
public View getInfoContents(Marker marker) {
   ...
   Object o = marker.getTag();
O-9
  • 1,626
  • 16
  • 15