27

Currently Android Map v2 snaps to marker location after click. I want to disable this behavior but see no options to do it.

Does anybody know how to fix that?

Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197
  • snap? what does it mean? please explain more detailed.. – BBonDoo Jan 24 '13 at 13:09
  • I am looking for a similar solution as well. my interpretation of "snap" is to have the camera move to that markers location. Basically how does one prevent the camera from moving when the user has clicked on a marker? – DMCApps Jan 24 '13 at 16:05

2 Answers2

53

Based on what I read from the Markers - Google Maps Android API (https://developers.google.com/maps/documentation/android/marker#marker_click_events)

Marker click events

You can use an OnMarkerClickListener to listen for click events on the marker. To set this listener on the map, call GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user clicks on a marker, onMarkerClick(Marker) will be called and the marker will be passed through as an argument. This method returns a boolean that indicates whether you have consumed the event (i.e., you want to suppress the default behavior). If it returns false, then the default behavior will occur in addition to your custom behavior. The default behavior for a marker click event is to show its info window (if available) and move the camera such that the marker is centered on the map.

You could likely override this method and have it only open the marker and return true to consume the event.

// Since we are consuming the event this is necessary to
// manage closing opened markers before opening new ones
Marker lastOpened = null;

mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    public boolean onMarkerClick(Marker marker) {
        // Check if there is an open info window
        if (lastOpened != null) {
            // Close the info window
            lastOpened.hideInfoWindow();

            // Is the marker the same marker that was already open
            if (lastOpened.equals(marker)) {
                // Nullify the lastOpened object
                lastOpened = null;
                // Return so that the info window isn't opened again
                return true;
            } 
        }

        // Open the info window for the marker
        marker.showInfoWindow();
        // Re-assign the last opened such that we can close it later
        lastOpened = marker;

        // Event was handled by our code do not launch default behaviour.
        return true;
    }
});

This is untested code but that may be a workable solution.

Thanks, DMan

DMCApps
  • 2,110
  • 4
  • 20
  • 36
  • Yeah! Thanks! In my case i just call the onMapClick(marker.getPosition()) so that the click on the marker acts like a click on the map at the marker's position. Then, return true to notify that the event was handled. – Thibault D. Feb 19 '13 at 14:11
  • very helpful, +1... though just FYI: it's spelled "opened" ;) also, this answer should be accepted op! – h4lc0n May 23 '13 at 10:51
  • @h4lc0n Thanks for the info (edited to reflect that) and the up vote/encouragement to be accepted! Maybe one day it will be! :D – DMCApps May 23 '13 at 18:31
  • The problem is that this workaround does not allow you to handle clicks somewhere near a marker (especially if the marker is big enough) as it will catch the click and you will get only marker center position by calling marker.getPosition(). – Ruslan Yanchyshyn Oct 31 '13 at 15:48
  • @RuslanYanchyshyn I don't quite understand what you are getting at. That sounds more like google maps is picking up that the marker was clicked vs just an area on the map. I don't think that's a flaw to this work around, just something that happens due to the inaccuracy of a press using a wide surface (typically your finger). – DMCApps Nov 04 '13 at 17:53
  • 1
    @RuslanYanchyshyn Check this question. Maybe you can set up an onTouchEvent for the screen, use getProjection to get the map coordinates and ignore the marker click with a flag if the click wasn't within a set +/- of the parkers position. http://stackoverflow.com/questions/4177305/get-coordinates-on-tapping-map-in-android/4177676#4177676 – DMCApps Nov 04 '13 at 17:57
  • If anyone's interested, I simplified DMan's excellent answer since the way it sets `lastOpened` to `null` is confusing: https://gist.github.com/Vincenator/65da1dd1b433ae33ee42 – VinceFior Jul 11 '14 at 04:40
  • @VinceFior your link doesn't work. Could you fix it? – Kai Sep 21 '14 at 15:42
  • 2
    @user714965 Sure, here's the working link: https://gist.github.com/VinceFior/65da1dd1b433ae33ee42 – VinceFior Sep 21 '14 at 17:19
  • @VinceFior I think the difference between my method and yours is that clicking on the opened pin in your code will NOT close the info window. Meanwhile my solution was that if the user clicks the currently opened pin that it would close the info dialog. Nonetheless +1 for a more simplified version with a similar result. – DMCApps Sep 27 '14 at 18:45
3

It appears we should be able to do the following, but due to this bug, it doesn't work because marker.isInfoWindowShown() always returns false:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    public boolean onMarkerClick(Marker marker) {
        if (marker.isInfoWindowShown()) {
            marker.hideInfoWindow();
        } else {
            marker.showInfoWindow();
        }
        return true;
    }
});

However, the following has the same effect and does work:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    Marker currentShown;

    public boolean onMarkerClick(Marker marker) {
        if (marker.equals(currentShown)) {
            marker.hideInfoWindow();
            currentShown = null;
        } else {
            marker.showInfoWindow();
            currentShown = marker;
        }
        return true;
    }
});

Because only one info window is displayed at a time (as stated in the Google Maps API v2 Developer Guide), we only have to bother about hiding the info window if the marker whose info window is currently open is clicked.

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36