简体   繁体   English

如何禁用android地图标记单击自动居中

[英]How to disable android map marker click auto center

I am looking for a way to disable my map fragment's auto centre on selected marker functionality.我正在寻找一种方法来禁用我的地图片段在选定标记功能上的自动居中。 I still want the markers InfoWindow to show up, but just not centre the entire map on the marker I have selected.我仍然希望标记 InfoWindow 显示出来,但只是不要将整个地图集中在我选择的标记上。

Take a look at the following post:看看下面的帖子:

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

There is a method given there by @DMan , basically you need to consume the OnMarkerClick event and override the default behavior: @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;
}
});

Simple way:简单的方法:

  1. Implement setOnMarkerClickListener()实现 setOnMarkerClickListener()
  2. Return 'TRUE' to prevent GoogleMap by default moves the map center to the marker.返回 '​​TRUE' 以防止默认情况下 GoogleMap 将地图中心移动到标记处。

Example:例子:

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

Very Simple:很简单的:

Use below code for implement setOnMarkerClickListener().使用下面的代码来实现 setOnMarkerClickListener()。

@Override
public boolean onMarkerClick(Marker marker) {

    marker.showInfoWindow(); // show info window

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

Simple.简单的。 Add moveOnMarkerPress prop to MapView and set it to false.moveOnMarkerPress添加到MapView并将其设置为 false。

 <MapView
      moveOnMarkerPress={false}
>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM