android google地图点聚合样式修改

如下

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>

 

 

 

在JavaScript中使用百度地图API实现聚合样式更换,通常需要使用百度地图聚合层(MarkerClusterer)插件。这个插件可以将地图上的多个标记进行聚合,在缩放级别较高时,显示为单个标记,而在缩放级别较低时,则会显示出聚合后的多个聚合样式可以通过插件提供的参数或者API进行自定义。 以下是一个简单的示例代码,展示如何使用百度地图的MarkerClusterer插件更换聚合样式: ```javascript // 创建地图实例 var map = new BMap.Map("container"); map.centerAndZoom(new BMap.Point(116.404, 39.915), 12); // 初始化地图,设置中心坐标和地图级别 // 创建标记数组 var markers = []; for (var i = 0; i < 100; i++) { var pt = new BMap.Point(116.404 + Math.random() * 0.01, 39.915 + Math.random() * 0.01); markers.push(new BMap.Marker(pt)); } // 创建聚合对象 var markerCluster = new BMap.MarkerClusterer(map, { grid Size: 60, // 聚合半径 max Zoom: 11 // 聚合最大层级 }); // 设置聚合后显示的单个标记样式 markerCluster.setStyles([ { "markerColor": "#ff0000", // 设置聚合的颜色 "markerSize": new BMap.Size(36, 36), // 设置聚合的大小 "markerSymbol": BMap.PinSymbol.RED // 设置聚合的图标样式 }, { "markerColor": "#00ff00", "markerSize": new BMap.Size(24, 24), "markerSymbol": BMap.PinSymbol.BLUE } ]); // 将标记添加到聚合对象中 markerCluster.add(markers); // 将聚合对象添加到地图上 markerCluster.addToMap(); ``` 在上述代码中,通过`markerCluster.setStyles`方法,可以设置不同缩放级别下的聚合样式,包括颜色、大小和图标等。注意,需要在调用`addToMap`方法之前设置聚合样式
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值