Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add identification to marker on google maps v2 api for android

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!

like image 431
vdrg Avatar asked Jan 16 '13 19:01

vdrg


People also ask

How do I add a marker on Google Maps Android?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.


4 Answers

You could make a HashMap<Marker, User>

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

like image 176
dumazy Avatar answered Oct 08 '22 13:10

dumazy


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.

like image 27
CommonsWare Avatar answered Oct 08 '22 15:10

CommonsWare


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.

like image 37
comeGetSome Avatar answered Oct 08 '22 15:10

comeGetSome


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.

like image 29
wapples Avatar answered Oct 08 '22 14:10

wapples