我正在尝试为我的 googlemaps 标记实现集群。目前我有一些自定义标记颜色,当您单击它们时会触发 API 调用。加载这些结果后,将打开一个底页,其中包含该标记的特定信息。
我想保留相同的功能(自定义标记/clickListeners/标记周围的半径),但在缩小时添加集群。我查看了不同的资源来寻求帮助:
但我不确定如何为集群项目实现自定义标记和侦听器。我能够在没有点击监听器的情况下获得带有标准标记的集群。以下是一些用于说明的图像:
这是我目前的情况(我想对这些标记进行聚类)。如您所见,当我单击标记时会弹出底部表格
这是我目前能够做到的,但我想将它与之前的图片结合起来
这是我的地图片段代码的重要部分,(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();
}
我希望有人可以在正确的方向上帮助我。谢谢!