Add identification to marker on google maps v2 api for android

23,813

Solution 1

You could make a HashMap<Marker, User>

check this tutorial: http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html

Solution 2

Is it possible to add an extra attribute to a marker object?

No. Marker is final. Also, the Marker objects you create vanish quickly, as they are only used for some IPC over to the Google Play Services app. The Marker object you get in your OnInfoWindowClickListener appears to be a reconstituted copy.

I have a subtitle in the snippet field so that's not an option.

Sure it is. Store the subtitle someplace else, and put your key to your user in the subtitle. When you render the InfoWindow from your InfoWindowAdapter, pull in the subtitle.

Solution 3

I dont think it is a good idea to keep strong references to markers via a map. Since you anyway use your custom window adapter to render contents, you could either "abuse" the snippet() or title() on the MarkerOptions to store your information. They are both strings, so depended on the information to store you would slightly use more memory, on the other side you'd be safe from memory leaks by holding strong references to markers.

also, you would go compatible to the way how Maps manages it persistency during stops and resumes.

Solution 4

Here is a slightly simpler solution I have implemented. All you do is Create a InfoWindowAdapter which takes something you want to pass to the window in it's constructor.

class CustomWindowAdapter implements InfoWindowAdapter{
LayoutInflater mInflater;
private HashMap<Marker, Double> mRatingHash;

public CustomWindowAdapter(LayoutInflater i, HashMap<Marker, Double> h){
    mInflater = i;
    mRatingHash = h;
}

@Override
public View getInfoContents(Marker marker) {
     // Getting view from the layout file
    View v = mInflater.inflate(R.layout.custom_info_window, null);

    TextView title = (TextView) v.findViewById(R.id.tv_info_window_title);
    title.setText(marker.getTitle());

    TextView description = (TextView) v.findViewById(R.id.tv_info_window_description);
    description.setText(marker.getSnippet());

    RatingBar rating = (RatingBar) v.findViewById(R.id.rv_info_window);
    Double ratingValue = mRatingHash.get(marker);
    rating.setRating(ratingValue.floatValue());
    return v;
}

@Override
public View getInfoWindow(Marker marker) {
    // TODO Auto-generated method stub
    return null;
}
}

You are responsible for whatever data you want to pass to the info window, but you can see here that I am passing a hash of ratings. Just a prototype and is by no means the best solution but this should get anyone started.

Share:
23,813
vdrg
Author by

vdrg

Updated on November 01, 2020

Comments

  • vdrg
    vdrg over 3 years

    Well, every marker on my application will represent a user, so I need to identify that user when I click the info window to get its data from the Internet, and I can't make it identify them by name for obvious reasons. Is it possible to add an extra attribute to a marker object? thanks!

  • TheLettuceMaster
    TheLettuceMaster about 10 years
    This should be marked the correct answer. Works perfect. Easy tutorial to follow.