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!
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.
You could make a HashMap<Marker, User>
check this tutorial: http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With