Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know which Marker was clicked on Google Maps v2 for Android?

I am making an application with a Google Maps on it, for Android. I have plenty of Markers on my screen and I am preparing customizable balloon for each marker when they are clicked. This means, I have information which is different according to the marker which was clicked.

I set up the contents of the View of the marker with setInfoWindowAdapter and then I override the method getInfoContents.

The problem is: This method is the general implementation of the contents of the Info Window, but each marker shall show its own information. So, as far as I understand, I have to somehow detect on getInfoContents(Marker marker) which of the markers have been clicked, in order to load from my data structures the necessary info to present on the info window. The question is: How do I identify what entity the clicked Marker 'marker' represents? I mean, having just the object Marker on getInfoContents which was triggered to show the info window, how can I detect which is the proper information to display? I though about comparing the string Title by using marker.getTitle(), but this obliges me to display a Title on the info window, which I do not want. There's also a marker.getId(), but such ID is generated by the API and I can't control it

Any ideas?

like image 293
filipehd Avatar asked May 11 '13 11:05

filipehd


People also ask

How do I find the marker ID on Google Maps?

getId() function to retrieve marker id.

What does a blue marker mean on Google Maps?

Google Maps Blue Pins Are For Locations You Searched Near.

What are Android markers?

Markers are objects of type Marker , and are added to the map with the GoogleMap. addMarker(markerOptions) method. Markers are designed to be interactive. They receive click events by default, and are often used with event listeners to bring up info windows.


2 Answers

In reply to:

The question is: How do I identify what entity the clicked Marker 'marker' represents? [...] There's also a marker.getId(), but such ID is generated by the API and I can't control it

You can control it. The marker is returned by addMarker(), so you can get its ID and store it. Here's the code:

Map <String, Integer> markers = new HashMap<String, Integer>();

...

MarkerOptions mo = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker))
.position(new LatLng(mLat, mLon))
.flat(true) 
.snippet("Click here for details")
.rotation(0)
.title(title);

When you add the marker to the map, store its ID on the container

MyClass myObject = new MyClass(lat, lon); // The class that you are rendering on the map with Markers, for example "Monument"
Marker mkr = map.addMarker(mo);
markers.put(mkr.getId(), myObject.getId()); 

Then when the marker is clicked, you can recover the id of "myObject" like this

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    public void onInfoWindowClick(Marker marker) {
        int id = markers.get(marker.getId());
        // Now id contains which Monument (or your preferred class) was clicked                         
    }
});
like image 67
voghDev Avatar answered Nov 15 '22 20:11

voghDev


There is a better option, and this is what google suggests:

Tag : An Object associated with the marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map<Marker, Object>. As another example, you can associate a String ID corresponding to the ID from a data set. Google Maps Android API neither reads nor writes this property.

Source

So you can do something like this:

for (ListItem it: mList) {
    Marker mk = mMap.addMarker(new MarkerOptions().position(it.getLatLng()).title(it.getName()).snippet(it.getAddress()));
    mk.setTag(it.getID());
}

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        Integer id = (Integer) marker.getTag();
        return false;
    }
});
like image 40
ShahiM Avatar answered Nov 15 '22 20:11

ShahiM