在 android map v2 中点击后不要捕捉到标记 [英] Don't snap to marker after click in android map v2

查看:23
本文介绍了在 android map v2 中点击后不要捕捉到标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前 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.

有人知道怎么解决吗?

推荐答案

基于我从 Markers - Google Maps 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 来监听标记上的点击事件.设置地图上的这个监听器,调用 GoogleMap.setOnMarkerClickListener(OnMarkerClickListener).当用户点击一个标记时,onMarkerClick(Marker) 将被调用并且标记将作为参数传递.此方法返回一个布尔值,指示是否您已经消费了该事件(即,您想抑制默认行为).如果它返回 false,那么除了您的自定义行为之外,还会发生默认行为.标记单击事件的默认行为是显示其信息窗口(如果可用)并移动相机,使标记在地图上居中.

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

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

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

谢谢,数据人

这篇关于在 android map v2 中点击后不要捕捉到标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆