Search code examples
androidandroid-maps-v2android-maps

Listener to map touch outside of the markers


I am using the google map api with some markers. I also overrode the infoWindow as below to show custom text.

public void setUpMap() {
    final GoogleMap map = mMapView.getMap();
    map.clear();
    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        private View mHolder;

        @Override
        public View getInfoWindow(final Marker marker) {
            Log.d("MAP", "Map clicked on marker = " + maker);
            etc....

this works fine and I change the icon of each marker when clicked making them visually selected. However, I want to "unselect" all markers. The problem is that I don't know how to add a listener that gets triggered from outside of the markers.

In other words, my listener "getInfoWindow" gets trigger only when a marker is touched. I want the opposite. Some sort of listener that tells me that the user touched the map but not the markers.

Can this be done easily? Any pointers are greatly appreciated.

thx!


Solution

  • Ok, I suppose that's really easy.
    You have OnMarkerClickListener and OnMapClickListener.

    So, in your case just register OnMapClickListener and in onMapClick() you can do what you need.

    One more thing - when you add markers, store them in Arraylist - then at any time you can do whatever is needed - even remove all markers from the map.