vdrg
vdrg

Reputation: 369

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!

Upvotes: 31

Views: 23912

Answers (5)

AlbertMarkovski
AlbertMarkovski

Reputation: 85

I am using an additional class to associate some information and functions with each marker. I don't think this is the best approach to it, but it is an option. Particularly if you want to have more than just information associated with each map marker. Here's the basic structure I use for this.

//   Make an array list of for all of your things
ArrayList<Thing> things;

class Thing {
    long thing_key;
    String thing_string;
    int thingRadius;
    Double coord_long;
    Double coord_lat;
    Marker marker;
}

//    Then to use this to start your list.
things = new ArrayList<>();

//   Create the thing object and save all the data
thing = new Thing();
thing.marker = thingMarker;
thing.thing_key = thing_key;
thing.thing_string = thing_string;
thing.radius = Integer.getInteger(thingRadius.getText().toString());

//    Save the thing to the thing ArrayList
things.add(thing);

Upvotes: 0

comeGetSome
comeGetSome

Reputation: 1963

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.

Upvotes: 5

wapples
wapples

Reputation: 181

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.

Upvotes: 2

dumazy
dumazy

Reputation: 14435

You could make a HashMap<Marker, User>

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

Upvotes: 38

CommonsWare
CommonsWare

Reputation: 1006674

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.

Upvotes: 8

Related Questions