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.

  • How to detect Region Enter/Exit for multiple beacons using AltBeacon android-beacon-library?
  • Android in Eclipse: Copy/Paste From LogCat (only “Text”)
  • Get screen width and height in a Fragment
  • Detecting the device being plugged in
  • Strange behavior using Java ternary operator on Android
  • Replacement for SimpleCursorAdapter?
  • 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?

    Related posts:

    Custom ImageView with drop shadow
    Android apply styles for different screens
    Get the IPaddress of the computer in an Android project using java
    Comparing files in Android Studio
    Need working solution to use Android Pattern Lock Screen in custom Application (and not source code ...
    How to check VPN connection status on Android ICS
  • How do I remove lines between ListViews on Android?
  • why I cannot use “run Android lint” from menu “Window” in Eclipse in toolbar I can run it, is it OK?
  • jQuery Mobile not running secondary javascript
  • what's the best way to apply a mask to an EditText on Android?
  • How to migrate from a paid android application to an application paid for with in-app billing?
  • How can you adjust Android SnackBar to a specific position on screen
  • 3 Solutions collect form web for “How to know which Marker was clicked on Google Maps v2 for Android?”

    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> mMarkers = 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

    Marker mkr = map.addMarker(mo);
    mMarkers.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 = mMarkers.get(marker.getId());
            // Do stuff with the id                         
        }
    });
    

    You are not obligated to show title when you have it set, so you can use that and as long as you return a View from getInfoContents and not setText on any subview of that returned View with title value.

    Depending on how and if you already keep references to all markers, there are alternatives, e.g. if you had List<Marker> policeMarkers and List<Marker> badGuysMarkers you can use a conditional if (policeMarkers.contains(marker)) { ... } else { ... }.

    You can also keep a Map<Marker, YourMarkerRelatedDataModel> allMarkers and do YourMarkerRelatedDataModel model = allMarkers.get(marker); and use that value to differentiate.

    Finally you can use Android Maps Extensions, which adds functions like Marker.setData(Object) and Object Marker.getData() to keep your model close to your markers and not create things like Map<Marker, Model>.

    try this code:

    @Override
        public void onInfoWindowClick(final Marker marker) 
        {
            // TODO Auto-generated method stub
           if (marker.getTitle().equalsIgnoreCase("Start")) {
            Toast.makeText(showMaps.this, "You have click on start -->",
                    Toast.LENGTH_LONG).show();
            Log.e("marker.getPosition()-->", "" + marker.getPosition());
          }
        }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.