如何禁用android地图标记点击自动中心

时间:2013-04-10 12:04:36

标签: android android-fragments google-maps-api-2

我正在寻找一种方法来禁用我的地图片段在所选标记功能上的自动中心。我仍然希望显示标记InfoWindow,但不要将整个地图置于我选择的标记上。

4 个答案:

答案 0 :(得分:53)

看看以下帖子:

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

@DMan 有一种方法,基本上你需要使用OnMarkerClick事件并覆盖默认行为:

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

答案 1 :(得分:31)

简单方法:

  1. 实施setOnMarkerClickListener()
  2. 返回'TRUE'以防止GoogleMap默认情况下将地图中心移动到标记。
  3. 示例:

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

答案 2 :(得分:5)

非常简单:

使用以下代码实现setOnMarkerClickListener()。

@Override
public boolean onMarkerClick(Marker marker) {

    marker.showInfoWindow(); // show info window

    return true; // can't move map by this
} 

答案 3 :(得分:1)

简单。将moveOnMarkerPress属性添加到MapView并将其设置为false。

 <MapView
      moveOnMarkerPress={false}
>