37

I am looking for a way to disable my map fragment's auto centre on selected marker functionality. I still want the markers InfoWindow to show up, but just not centre the entire map on the marker I have selected.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
FiniteRed
  • 810
  • 3
  • 10
  • 20
  • It is working properly, it is exactly proper solution. [enter link description here][1] [1]: http://stackoverflow.com/questions/15925319/how-to-disable-android-map-marker-click-auto-center?answertab=active#tab-top – Amitabha Biswas Aug 15 '14 at 11:05

4 Answers4

55

Take a look at the following post:

Don't snap to marker after click in android map v2

There is a method given there by @DMan, basically you need to consume the OnMarkerClick event and override the default behavior:

// Since we are consuming the event this is necessary to
// manage closing openned markers before openning new ones
Marker lastOpenned = null;

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

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

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

    // Event was handled by our code do not launch default behaviour.
    return true;
}
});
Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • @Emil Adz,I have used the same logic but still i got map move onMarker Click; – Aravintha Bashyam.c Nov 26 '13 at 09:26
  • @AravinthaBashyam.c if you are returning true at the end on this method then it shouldn't move, and why is the down-vote? – Emil Adz Nov 26 '13 at 09:36
  • @AravinthaBashyam.c I think you had written setOnMarkerOnclick many times, – Sridhar Nov 26 '13 at 10:10
  • 3
    What do you mean, the answer is perfectly fine and been upvoted by 5 users, if it doesn't work for you then you are doing something wrong, open a new question with your code and I will try to help you. – Emil Adz Nov 26 '13 at 10:18
36

Simple way:

  1. Implement setOnMarkerClickListener()
  2. Return 'TRUE' to prevent GoogleMap by default moves the map center to the marker.

Example:

map.setOnMarkerClickListener(
    new OnMarkerClickListener() {
        boolean doNotMoveCameraToCenterMarker = true;
        public boolean onMarkerClick(Marker marker) {
            //Do whatever you need to do here ....
            return doNotMoveCameraToCenterMarker;
        }
    });
Panini Luncher
  • 639
  • 8
  • 10
8

Very Simple:

Use below code for implement setOnMarkerClickListener().

@Override
public boolean onMarkerClick(Marker marker) {

    marker.showInfoWindow(); // show info window

    return true; // can't move map by this
} 
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
Vishal Jadav
  • 924
  • 9
  • 11
3

Simple. Add moveOnMarkerPress prop to MapView and set it to false.

 <MapView
      moveOnMarkerPress={false}
>
Sudarshana Dayananda
  • 5,165
  • 2
  • 23
  • 45
woodrejs
  • 141
  • 1
  • 2