不要捕捉点击在Android的地图V2后标记标记、地图、Android

2023-09-04 03:31:08 作者:等一个绿灯

目前的Andr​​oid地图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?

推荐答案

根据我从标记读取 - 谷歌地图Android的API(https://developers.google.com/maps/documentation/android/marker#marker_click_events)

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

标记的单击事件

您可以使用OnMarkerClickListener监听的标记click事件。设置       这个监听器在地图上,调用GoogleMap.setOnMarkerClickListener(OnMarkerClickListener)。       当用户点击一个标记,onMarkerClick(标记)将被调用,则标记开始       可以通过作为参数。该方法返回一个布尔值,指示是否       你已经消耗的情况下(例如,你想晚饭preSS的默认行为)。如果它       返回false,那么默认的行为将发生在除了你的自定义行为。       一个标记click事件的默认行为是显示其信息窗口(如果有的话)       和移动相机,使得标记物为中心在地图上。

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.

您可能会覆盖此方法,让它只开放标记,并返回true消费事件。

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;
    }
});

这是未经考验code,但可能是一个可行的解决方案。

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

谢谢, DMAN