在android map v2中单击后不要捕捉到标记

Ale*_*rov 24 android google-maps android-mapview android-maps-v2

目前Android Map v2在点击后捕捉到标记位置.我想禁用此行为,但看不到任何选项.

有人知道如何解决这个问题吗?

DMC*_*pps 51

基于我从Markers中读到的内容 - Google Maps Android API(https://developers.google.com/maps/documentation/android/marker#marker_click_events)

标记点击事件

您可以使用OnMarkerClickListener侦听标记上的单击事件.要在地图上设置此侦听器,请调用GoogleMap.setOnMarkerClickListener(OnMarkerClickListener).当用户单击标记时,将调用onMarkerClick(Marker)并将标记作为参数传递.此方法返回一个布尔值,指示您是否已使用该事件(即,您要禁止默认行为).如果返回false,则除了自定义行为外,还会发生默认行为.标记单击事件的默认行为是显示其信息窗口(如果可用)并移动摄像机,使标记在地图上居中.

您可能会覆盖此方法并让它只打开标记并返回true以使用该事件.

// 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;
    }
});
Run Code Online (Sandbox Code Playgroud)

这是未经测试的代码,但这可能是一个可行的解决方案.

谢谢,DMan

  • @ user714965当然,这是工作链接:https://gist.github.com/VinceFior/65da1dd1b433ae33ee42 (2认同)