如何禁用android地图标记点击自动中心时,使用集群



我知道我可以在点击标记时避免相机居中:

mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        marker.showInfoWindow();
        return true;
    }
});

然而,我的问题是,现在我想使用集群,当然,我想看到信息窗口时,标记(实际上,项目或集群)被录音。但我不知道在那些情况下如何调用像showindowwindow这样的东西。在这段代码中,如果我返回false,我将看到信息窗口,但是map居中。如果我返回true,地图不居中,但信息窗口不显示:

    mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<HotspotMarker>() {
        @Override
        public boolean onClusterItemClick(HotspotMarker item) {
            Log.e(LOG_TAG, "---OnClusterItemClick");
            return false;
        }
    });
    mClusterManager.setOnClusterItemInfoWindowClickListener(new ClusterManager.OnClusterItemInfoWindowClickListener<HotspotMarker>() {
        @Override
        public void onClusterItemInfoWindowClick(HotspotMarker hotspotMarker) {
            Log.e(LOG_TAG, "---onClusterItemInfoWindowClick");
            HotspotDetailActivity.hotspot = hotspotMarker.getHotspot();
            Intent intent = new Intent(getActivity(), HotspotDetailActivity.class);
            startActivity(intent);
        }
    });
    mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<HotspotMarker>() {
        @Override
        public boolean onClusterClick(Cluster<HotspotMarker> cluster) {
            Log.e(LOG_TAG, "---OnClusterClickListener");
            return false;
        }
    });
    mClusterManager.setOnClusterInfoWindowClickListener(new ClusterManager.OnClusterInfoWindowClickListener<HotspotMarker>() {
        @Override
        public void onClusterInfoWindowClick(Cluster<HotspotMarker> cluster) {
            Log.e(LOG_TAG, "---onClusterInfoWindowClick");
            ArrayList<HotspotMarker> markers = new ArrayList<>(cluster.getItems());
            ClusterListActivity.markers = markers;
            Intent detailHotspot = new Intent(getActivity(), ClusterListActivity.class);
            startActivity(detailHotspot);
        }
    });

你知道当我使用集群时,如何避免地图居中,同时显示信息窗口?提前谢谢。

编辑:使用@skywall的建议,我发现了一个更好的方法使用自定义渲染器:

mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<HotspotMarker>() {
            @Override
            public boolean onClusterItemClick(HotspotMarker item) {
                Marker marker=mClusteringRenderer.getMarker(item);
                marker.showInfoWindow();
                return true;
            }
        });
    mClusterManager.setOnClusterItemInfoWindowClickListener(new ClusterManager.OnClusterItemInfoWindowClickListener<HotspotMarker>() {
        @Override
        public void onClusterItemInfoWindowClick(HotspotMarker hotspotMarker) {
            HotspotDetailActivity.hotspot = hotspotMarker.getHotspot();
            Intent intent = new Intent(getActivity(), HotspotDetailActivity.class);
            startActivity(intent);
        }
    });
    mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<HotspotMarker>() {
        @Override
        public boolean onClusterClick(Cluster<HotspotMarker> cluster) {
            Marker marker=mClusteringRenderer.getMarker(cluster);
            marker.showInfoWindow();
            return true;
        }
    });
    mClusterManager.setOnClusterInfoWindowClickListener(new ClusterManager.OnClusterInfoWindowClickListener<HotspotMarker>() {
        @Override
        public void onClusterInfoWindowClick(Cluster<HotspotMarker> cluster) {
            ArrayList<HotspotMarker> markers = new ArrayList<>(cluster.getItems());
            ClusterListActivity.markers = markers;
            Intent detailHotspot = new Intent(getActivity(), ClusterListActivity.class);
            startActivity(detailHotspot);
        }
    });

return true for setOnClusterItemClickListener

禁止直接通过mGoogleMap.setOnMarkerClickListener()设置GoogleMap.OnMarkerClickListener()的新实例。

相反,将mClusterManager设置为单击处理程序,如下所示:

mGoogleMap.setOnMarkerClickListener(mClusterManager);

和处理点击:

mClusterManager.setOnClusterItemClickListener();
mClusterManager.setOnClusterClickListener();

InfoWindow默认显示在第一次点击。

EDIT:我现在明白了,问题在哪里。我创造了一个不漂亮的解决方案,但它有效。这样修改你的点击监听器:

mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyClusterItem>() {
        @Override
        public boolean onClusterItemClick(MyClusterItem item) {
            for (Marker marker : mClusterManager.getMarkerCollection().getMarkers()) {
                if (marker.getPosition().latitude == item.getPosition().latitude && 
                    marker.getPosition().longitude == item.getPosition().longitude) {
                    marker.showInfoWindow();
                }
            }
            return true;
        }
    });

我不能禁用,但我可以移动下面的地图(它需要在我的情况下,因为屏幕的上半部分被一些抽屉覆盖)。

public View getInfoContents(Marker marker) {
    .....
    bottomBarButton1.postDelayed(new Runnable() {
        @Override
        public void run() {
            final CameraPosition cp=map.getCameraPosition();
            final Point mappoint = map.getProjection().toScreenLocation(new LatLng(cp.target.latitude, cp.target.longitude));
            mappoint.set(mappoint.x, (int) (dispHeight*0.1f));
            map.animateCamera(CameraUpdateFactory.newLatLng(map.getProjection().fromScreenLocation(mappoint)), null);
        }
    },500);
    .....

}

相关内容

  • 没有找到相关文章

最新更新