Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 985
Alexey Zakharov Avatar asked Jan 24 '13 09:01

Alexey Zakharov


People also ask

How do I change the marker on Google Maps Android?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How do you drag and drop a red marker on a map?

Place a tick in the small box on the Location – Marker is placed incorrectly on the map line. Do this by placing the cursor over the box and left click. You will now be able to drag the marker to the correct position. This is done by placing your cursor over the red marker, left click and hold the mouse button down.


2 Answers

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

like image 55
DMCApps Avatar answered Sep 19 '22 01:09

DMCApps


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.

like image 24
HexAndBugs Avatar answered Sep 22 '22 01:09

HexAndBugs