Android:如果标记在地图上相互重叠,则会针对最后一个隐藏的标记触发click事件

时间:2014-02-07 06:09:34

标签: android google-maps click overlap marker

在我们的应用程序中,很多标记在不同的位置绘制,在某些情况下,在某个缩放级别,标记相互重叠。因此,当我点击标记时,我希望顶部标记的onMarkerClick被触发,而是针对最后一个隐藏标记,即最后一个标记触发它,后面没有标记。< / p>

你建议我做什么?此外,我没有信息窗口,因此我从true方法返回onMarkerClick

2 个答案:

答案 0 :(得分:2)

我在这里找到了解决方案:https://github.com/googlemaps/android-maps-utils/issues/26

mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()
    {
        @Override
        public View getInfoWindow(Marker marker)
        {
            // Empty info window.
            return new View(getApplicationContext());
        }

        @Override
        public View getInfoContents(Marker marker)
        {
            return null;
        }
    });

答案 1 :(得分:0)

  1. 确定制作者相对于整个屏幕的大小(例如10%)。将其定义为名为IMAGE_SIZE_RATIO

  2. 的浮点数
  3. 在向地图添加标记时维护列表标记。在OnMarkerClickListener onMarkerClick方法中,迭代其他标记并比较标记之间相对于当前可见地图维度和标记大小的距离。如以下示例代码所示:

  4.     static final float IMAGE_SIZE_RATIO = .15f; //define how big your marker is relative to the total screen
        private void setupListeners() {
        getMap().setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            LatLngBounds b = getMap().getProjection().getVisibleRegion().latLngBounds;
            Float distance = distanceBetweenPoints(b.northeast, b.southwest) * IMAGE_SIZE_RATIO;
            for (Marker m : makers ) {
                if (marker.equals(m) ) { continue; } //don't compare the same one
                if (distanceBetweenPoint(m.getPosition(), marker.getPosition()) {
                    /*Note do onMarkerClick this as an Aynch task and continue along 
                    if also want to fire off against the main object.
                    */
                    return onMarkerClick(m);
                }
            }
            // do the operation you want on the actual marker.
        }
        ....(remainder of listener)...      
        }       
        }
        protected Float getDistanceInMeters(LatLng a, LatLng b) {
            Location l1 = new Location("");
            Location l2 = new Location("");
            l1.setLatitude(a.latitude);
            l1.setLongitude(a.longitude);
            l2.setLatitude(b.latitude);
            l2.setLongitude(b.longitude);
            return l1.distanceTo(l2)
        }