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

时间:2015-07-17 07:36:16

标签: android google-maps google-maps-markers google-maps-android-api-2 android-maps-utils

我知道使用以下方法点击标记时可以避免使相机居中:

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

但是,我遇到的问题是现在我想使用群集,当然我想在标记(实际上是项目或群集)时才能看到信息窗口。但我不知道在这些情况下如何调用类似showInfoWindow的东西。 在这段代码中,如果我返回false,我会看到信息窗口,但是map是居中的。如果我返回true,则map不居中,但不显示info窗口:

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

3 个答案:

答案 0 :(得分:6)

return true

setOnClusterItemClickListener

答案 1 :(得分:2)

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

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

mGoogleMap.setOnMarkerClickListener(mClusterManager);

并处理点击次数:

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

首次点击时默认显示InfoWindow。

编辑:我现在明白了问题所在。我创造的并不是很漂亮的解决方案,但它确实有效。以这种方式修改您的点击监听器:

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

答案 2 :(得分:0)

我无法禁用,但我可以在下方移动地图(在我的情况下需要,因为某些抽屉覆盖了屏幕的上半部分)。

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

}