Adding custom property to marker Google Map Android API V2

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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);

}

Answers

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.

http://code.google.com/p/android-maps-extensions/ http://code.google.com/p/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();

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/16997130/adding-custom-property-to-marker-google-map-android-api-v2

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils