Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between different markers in Maps API v2 (unique identifiers)

I have an ArrayList of a custom class. There are about 10 objects in the list, each with details like Title, Snippet, LatLng. I have successfully added all 10 to a Map by using my custom class functions like getTitle, getSnippet, getLatLng etc.

Now, when I click the info window (of the marker), I want to be able to somehow KNOW which object of my custom class does that marker correspond to.

For example, if I click the McDonald's market, I want to be able to know which item from my ArrayList did that marker belong to.

I've been looking at the MarkerOptions and there doesn't seem to be anything in there that I can use to identify the relevant custom object with.

If the question is too confusing, let me make it simple:

ArrayList<CustomObj> objects = blah
map.addMarker(new MarkerOptions().position(new LatLng(
                            Double.parseDouble(result.get(i).getCompanyLatLng()
                                    .split(",")[0]), Double.parseDouble(result
                                    .get(i).getCompanyLatLng().split(",")[1])))
                                    .title(result.get(i).getCompanyName())
                                    .snippet(result.get(i).getCompanyType())
                                    .icon(BitmapDescriptorFactory
                                            .fromResource(R.drawable.pin)));

Now when this is clicked, I go on to the next page. The next page needs to know WHICH object was clicked so that I can send the other details to that page (e.g. Image URLs that need to be loaded etc).

How do I add a unique integer or any identifier to my marker?

like image 548
Asim Avatar asked May 23 '13 12:05

Asim


1 Answers

One correct way is to use Map<Marker, CustomObj> which stores all markers:

Marker marker = map.addMarker(...);
map.put(marker, result.get(i));

and in onInfoWindowClick:

CustomObj obj = map.get(marker);

Another try is using Android Maps Extensions, which add getData and setData method to Marker, so you can assign your CustomObj object after creating marker and retrieve it in any callback.

like image 122
MaciejGórski Avatar answered Oct 05 '22 20:10

MaciejGórski