如下
public class MyGoogleItem implements GoogleClusterItem { public final LatLng mPosition; public int statue; public CarListData carInfo; public String snippet=""; public int profilePhoto; public MyGoogleItem(LatLng latLng, int statue, CarListData info) { this.mPosition = latLng; this.statue = statue; this.carInfo = info;
}
@Override
public LatLng getPosition() {
return this.mPosition;
}
@Override
public String getTitle() {
return "";
}
@Override
public String getSnippet() {
return snippet;
}
@Override
public int getProfilePhoto() {
if (statue == 2) {
return R.mipmap.monitor_che_icon17;
} else if (statue == 3) {
return R.mipmap.monitor_che_icon9;
} else if (statue == 4) {
return R.mipmap.monitor_che_icon38;
} else if (statue == 5) {
return R.mipmap.monitor_che_icon1;
} else {
return R.drawable.loation_start;
}
}
}
先自定义MyGoogleItem。设置图片样式:getProfilePhoto,
private void initGoogleMap() { try { MapsInitializer.initialize(getActivity()); } catch (Exception e) { e.printStackTrace(); } int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getActivity()); LogPlus.e("errorcode=" + errorCode); if (ConnectionResult.SUCCESS != errorCode) { GooglePlayServicesUtil.getErrorDialog(errorCode, this.getActivity(), 0).show(); } else { mMapView.getMapAsync(this); if (mGoogleMap != null) { LogPlus.e("mGoogleMap=" + mGoogleMap); // initVolley(); if (LocationPoint != null) { LatLng india = new LatLng(LocationPoint.latitude, LocationPoint.longitude); mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(india, 9)); }
mClusterManager = new ClusterManager<>(this.getActivity(), mGoogleMap);
mClusterManager.setRenderer(new CarRenderer());
// mClusterManager.setAnimation(true);
// Point the map's listeners at the listeners implemented by the cluster
// manager.点聚合监听
mGoogleMap.setOnCameraIdleListener(mClusterManager);
mGoogleMap.setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterItemClickListener(item -> { startActivity(CarDetailActivity.newIntent(getActivity(), item.carInfo.carInfo.getCarType(), item.carInfo.carInfo.GUID)); return true; }); mClusterManager.setOnClusterClickListener(cluster -> { float level = mGoogleMap.getCameraPosition().zoom; mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LocationPoint, level +2)); return true; }); } } }
重要的是设置setRenderer()
;
然后新建CarRenderer();
private class CarRenderer extends DefaultClusterRenderer<MyGoogleItem> { private final IconGenerator mIconGenerator = new IconGenerator(MyApplication.getInstance()); private final IconGenerator mClusterIconGenerator = new IconGenerator(MyApplication.getInstance()); private final ImageView mImageView; private final ImageView mClusterImageView; private final int mDimension;
public CarRenderer() { super(MyApplication.getInstance(), getMap(), mClusterManager); View multiProfile = getLayoutInflater().inflate(R.layout.multi_profile, null); mClusterIconGenerator.setContentView(multiProfile); mClusterImageView = multiProfile.findViewById(R.id.image); mImageView = new ImageView(MyApplication.getInstance()); mDimension = (int) getResources().getDimension(R.dimen.custom_profile_image); mImageView.setLayoutParams(new ViewGroup.LayoutParams(mDimension, mDimension)); int padding = (int) getResources().getDimension(R.dimen.custom_profile_padding); mImageView.setPadding(padding, padding, padding, padding); mIconGenerator.setContentView(mImageView); }
@Override
protected void onBeforeClusterItemRendered(@NonNull MyGoogleItem person, MarkerOptions markerOptions) {
// Draw a single person - show their profile photo and set the info window to show their name
markerOptions.icon(getItemIcon(person)).title(person.snippet);
}
private BitmapDescriptor getItemIcon(MyGoogleItem person) { // mImageView.setImageResource(person.getProfilePhoto()); mImageView.setBackground(getActivity().getResources().getDrawable(person.getProfilePhoto())); // mImageView.setBackground(getActivity().getResources().getDrawable(person.profilePhoto)); Bitmap icon = mIconGenerator.makeIcon(); return BitmapDescriptorFactory.fromBitmap(icon); } @Override protected void onBeforeClusterRendered(@NonNull Cluster<MyGoogleItem> cluster, MarkerOptions markerOptions) { super.onBeforeClusterRendered(cluster,markerOptions); // Draw multiple people. // Note: this method runs on the UI thread. Don't spend too much time in here (like in this example). // markerOptions.icon(getClusterIcon(cluster)); }
@Override protected boolean shouldRenderAsCluster(Cluster cluster) { // Always render clusters. /* if(mGoogleMap.getCameraPosition().zoom>=18.0) { return false; }else { return cluster.getSize() > 1; }*/ return cluster.getSize() > 1; } // @Override protected String getClusterText(int bucket) { if (bucket < 1000) { return String.valueOf(bucket); } return "999+"; }
@Override protected int getBucket(Cluster cluster) { int size = cluster.getSize(); return size; /* if (size <= BUCKETS[0]) { return size; } else { for(int i = 0; i < BUCKETS.length - 1; ++i) { if (size < BUCKETS[i + 1]) { return BUCKETS[i]; } } return BUCKETS[BUCKETS.length - 1]; }*/ }
}
CarRenderder里就是设置各种聚合样式,marker点样式:包括显示数据:
multi_profile 即为layout样式marker点样式设置。
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="2dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/image" android:layout_width="@dimen/custom_profile_image" android:layout_height="@dimen/custom_profile_image" android:src="@mipmap/monitor_che_icon17" />
</FrameLayout>