1

我正在尝试为我的 googlemaps 标记实现集群。目前我有一些自定义标记颜色,当您单击它们时会触发 API 调用。加载这些结果后,将打开一个底页,其中包含该标记的特定信息。

我想保留相同的功能(自定义标记/clickListeners/标记周围的半径),但在缩小时添加集群。我查看了不同的资源来寻求帮助:

Android 集群和标记点击次数

Android 标记聚类

标记管理器.java

但我不确定如何为集群项目实现自定义标记和侦听器。我能够在没有点击监听器的情况下获得带有标准标记的集群。以下是一些用于说明的图像:

这是我目前的情况(我想对这些标记进行聚类)。如您所见,当我单击标记时会弹出底部表格

现状

这是我目前能够做到的,但我想将它与之前的图片结合起来

聚类图

这是我的地图片段代码的重要部分,(Point该类确实实现了ClusterItem接口):

private Map<Marker, Point> retailerInfo = new HashMap<>();

    private void markGeofencesOnMap() {
        new GeofenceAreasRequest().getAllAreas(new GeofenceAreasCallback() {
            @Override
            public void onAreasLoaded(List<Point> points) {
                for (final Point point : points) {
                    markerForGeofence(point);
                    drawRadius(point);
                }
            }
            @Override
            public void failedOnAreasLoaded(int message) {
                Snackbar.make(coordinatorLayout, R.string.failed_loading_areas, Snackbar.LENGTH_LONG).show();
            }
        });
    }

    private void markerForGeofence(Point point) {
        LatLng latLng = new LatLng(point.getLatitude(), point.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions()
                .position(latLng)
                .flat(true)
                .title(point.getTitle())
                .icon(BitmapDescriptorFactory.defaultMarker(195));
//        markerManager.getCollection("markerCollection").addMarker(markerOptions);
//        markerManager.getCollection("markerCollection").setOnMarkerClickListener(this);
//        mClusterManager.addItem(point);
        geoFenceMarker = googleMap.addMarker(markerOptions);
        retailerInfo.put(geoFenceMarker, point);
    }

    private void drawRadius(Point point) {
        CircleOptions circleOptions = new CircleOptions()
                .center(geoFenceMarker.getPosition())
                .strokeColor(Color.argb(50, 70, 70, 70))
                .fillColor(Color.argb(100, 150, 150, 150))
                .radius(point.getRadius());
        googleMap.addCircle(circleOptions);
    }

    private void googleMapSettings() {
        int permissionCheck = ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION);
        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            googleMap.setMyLocationEnabled(true);
        } else {
            SystemRequirementsChecker.checkWithDefaultDialogs(getActivity());
        }
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        CameraPosition cameraPosition = new CameraPosition.Builder().target(getLastLocation()).zoom(12).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        googleMap.setOnMapClickListener(this);
//        markerManager = new MarkerManager(googleMap);
//        markerManager.newCollection("markerCollection");
//        mClusterManager = new ClusterManager<Point>(getActivity(), googleMap );//, markerManager);
//        googleMap.setOnMarkerClickListener(mClusterManager); //markerManager);
        googleMap.setOnCameraIdleListener(mClusterManager);
        googleMap.setOnMarkerClickListener(this);
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        if (retailerInfo != null) {
            String retailerId = retailerInfo.get(marker).getRetailer();
            new RetailersRequest().getRetailer(retailerId, new RetailersCallback() {
                @Override
                public void onRetailersLoad(List<Retailer> retailers) {
                    for (final Retailer retailer : retailers) {
                        mMapRetailerName.setText(retailer.getName());
                        mMapRetailerStreet.setText(retailer.getStreet());
                        mMapRetailerHouseNr.setText(retailer.getHousenumber());
                        mMapRetailerPostalCode.setText(retailer.getPostalCode());
                        mMapRetailerCity.setText(retailer.getCity());
                    }
                    bottomSheet.setVisibility(View.VISIBLE);
                    mBottomSheetBehavior.setPeekHeight(400);
                    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }

                @Override
                public void failedOnRetailersLoaded(int code) {
                    Snackbar.make(coordinatorLayout, getString(R.string.failed_loading_retailers) + code, Snackbar.LENGTH_LONG).show();
                }
            });
            CameraPosition cameraPosition = new CameraPosition.Builder().target(marker.getPosition()).zoom(14).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
        return true;
    }

    @Override
    public void onMapClick(LatLng latLng) {
        bottomSheet.setVisibility(View.GONE);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
        googleMapSettings();
        markGeofencesOnMap();
    }

我希望有人可以在正确的方向上帮助我。谢谢!

4

1 回答 1

0

好的,在经历了更多的尝试和错误之后,我想我已经解决了大部分问题。我所要做的就是将自定义附加DefaultClusterRenderer到我的 clusterManager。在 mapfragment 中,我可以onClusterItemClick用来处理标记点击。我仍然遇到的一个问题是放大和缩小时圆圈并没有正确渲染。

private void markGeofencesOnMap() {
    new GeofenceAreasRequest().getAllAreas(new GeofenceAreasCallback() {
        @Override
        public void onAreasLoaded(List<Point> points) {
            for (final Point point : points) {
                mClusterManager.addItem(point);
            }
        }
    });
}

private void googleMapSettings() {        
    mClusterManager = new ClusterManager<Point>(getActivity(), googleMap );
    // attach custom renderer behaviour
    mClusterManager.setRenderer(new OwnPointRendered(getActivity().getApplicationContext(), googleMap, mClusterManager)); 
    mClusterManager.setOnClusterItemClickListener(this);
    googleMap.setOnMarkerClickListener(mClusterManager);
    googleMap.setOnCameraIdleListener(mClusterManager);
}

@Override
public boolean onClusterItemClick(ClusterItem clusterItem) {
    // cast ClusterItem to my Point class to handle marker clicks
    Point retailer = (Point) clusterItem;
    String retailerId = retailer.getRetailer();
    return true;
}

OwnPointRendered.class

public class OwnPointRendered extends DefaultClusterRenderer<Point> {
private final GoogleMap map;
private List<Circle> circleList = new ArrayList<>();

public OwnPointRendered(Context context, GoogleMap map,
                        ClusterManager<Point> clusterManager) {
    super(context, map, clusterManager);
    this.map = map;
}

@Override
protected void onBeforeClusterItemRendered(Point item, MarkerOptions markerOptions) {
    markerOptions.flat(true);
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(195));
    drawRadius(item);
    super.onBeforeClusterItemRendered(item, markerOptions);
}

@Override
protected void onClusterRendered(Cluster<Point> cluster, Marker marker) {
    super.onClusterRendered(cluster, marker);
    for (Circle circle : circleList) {
        circle.remove();
    }
    circleList.clear();
}

private void drawRadius(Point point) {
    CircleOptions circleOptions = new CircleOptions()
            .center(point.getPosition())
            .strokeColor(Color.argb(50, 70, 70, 70))
            .fillColor(Color.argb(100, 150, 150, 150))
            .radius(point.getRadius());
    Circle circle = map.addCircle(circleOptions);
    circleList.add(circle);
}
于 2017-03-16T09:19:53.553 回答