Appearance
点聚合
基于 MapLibre GeoJsonSource 原生聚合能力实现的点聚合模块。通过 CircleLayer(聚合圆圈)+ SymbolLayer(聚合数字 / 单点图标)渲染,缩放/平移地图时由 MapLibre 引擎自动重新聚类,无需手动重算。
涉及类全部位于包 com.sfmap.api.maps.cluster 下:
| 类 / 接口 | 说明 |
|---|---|
ClusterOverlay | 点聚合 Overlay,核心入口,负责数据管理、渲染与点击事件分发 |
ClusterItem | 聚合点接口,业务方实现以提供每个点的坐标 |
ClusterStyleOptions | 聚合样式配置(圆圈、数字、单点图标),链式 setter |
Cluster | 聚合点集合,包含一组 ClusterItem |
ClusterClickListener | 聚合点点击监听接口 |
ClusterRender | 聚合渲染器接口,业务方实现以提供聚合 Marker 样式 |
ClusterColorStop | 聚合圆圈颜色分段项(按聚合数量分段着色) |
ClusterRadiusStop | 聚合圆圈半径分段项(按聚合数量分段设置半径) |
ClusterItem
com.sfmap.api.maps.cluster.ClusterItem
聚合点接口,由业务方实现,提供每个点的位置信息。
| 方法签名 | 说明 |
|---|---|
LatLng getPosition() | 返回该点的经纬度坐标 |
ClusterOverlay
com.sfmap.api.maps.cluster.ClusterOverlay
点聚合 Overlay。基于 MapLibre GeoJsonSource 原生聚合能力,通过 CircleLayer + SymbolLayer 渲染聚合圆和数字。缩放/平移地图时由 MapLibre 引擎自动重新聚类,无需手动重算。
构造方法
| 方法签名 | 说明 |
|---|---|
ClusterOverlay(MapController mapController, int clusterSize, Context context) | 基础构造,绑定地图控制器并指定聚合半径(clusterSize,实际传给 GeoJsonSource 的 clusterRadius 为 clusterSize / 2),构造完成后立即注册地图点击监听(用于命中聚合圆/单点) |
ClusterOverlay(MapController mapController, List<ClusterItem> clusterItems, int clusterSize, Context context) | 在基础构造之上批量注入初始聚合点列表,并立即执行一次重聚类(recluster()) |
公共方法
| 方法签名 | 说明 |
|---|---|
void setOnClusterClickListener(ClusterClickListener listener) | 设置聚合点点击监听器;点击聚合圆或单点时回调 ClusterClickListener.onClick |
void setClusterRender(ClusterRender clusterRender) | 设置聚合渲染器(业务方实现 ClusterRender 以提供聚合 Marker 样式) |
void setClusterRenderer(ClusterRender clusterRender) | setClusterRender 的别名,行为一致 |
void setClusterStyleOptions(ClusterStyleOptions styleOptions) | 设置聚合样式配置(聚合圆圈、聚合数字、单点图标的属性),传 null 恢复默认样式;下次重聚类时生效,需立即刷新可调用 updateClusters() |
void addClusterItem(ClusterItem item) | 追加单个聚合点并立即重聚类;item 为 null 时忽略 |
void updateClusterItems(List<ClusterItem> items) | 用给定列表全量替换当前聚合点(先清空再添加)并重聚类;items 为 null 时仅清空 |
void setClusterVisibleOnly(boolean clusterVisibleOnly) | 设置是否仅聚合可见区域内的点 |
void setCenterMapToClickedMarker(boolean centerMapToClickedMarker) | 设置点击聚合点后是否自动将地图居中到点击位置;开启后点击聚合圆会放大两级并居中,点击单点/Marker 会 moveCamera 居中 |
void updateClusters() | 触发一次重聚类(等价于内部 recluster()),用于样式或数据变更后立即刷新 |
List<Cluster> getClusters() | 返回当前聚合结果。原生聚合模式下返回包含全部 clusterItems 的单个 Cluster |
void responseClusterClickEvent(Marker marker) | 手动触发聚合点击事件:回调 ClusterClickListener.onClick,若开启居中则 moveCamera 到 Marker 位置 |
void onDestroy() | 销毁:从地图 Style 中移除聚合相关 layer/source/image,并清空聚合点列表与当前聚合结果 |
点击行为说明
构造时通过 MapLibreMap.addOnMapClickListener 注册地图点击监听(不覆盖用户自身的 OnMapClickListener)。点击时依次命中测试:
- 先查聚合圆 CircleLayer:命中则回调
onClick(null, [], count, pos),其中count为聚合点数量(取自point_count属性,未知时为-1);若开启居中,则animateCamera放大两级(zoom+2)并居中到点击点。 - 再查单点 SymbolLayer:命中则回调
onClick(null, [], 1, pos)。
ClusterStyleOptions
com.sfmap.api.maps.cluster.ClusterStyleOptions
点聚合样式配置。聚合层由三部分组成,均可通过本类链式 setter 配置:
- 聚合圆圈(CircleLayer):颜色可按聚合点数量分段、半径、描边颜色/宽度、透明度
- 聚合数字(SymbolLayer):字号、颜色、光晕颜色/宽度、字体
- 单点图标(SymbolLayer,非聚合点):图标 Bitmap、图标大小,或通过
MarkerOptions模板统一配置
未设置的属性使用默认值。通过 ClusterOverlay.setClusterStyleOptions(ClusterStyleOptions) 应用,应用后下次重聚类生效,也可调用 ClusterOverlay.updateClusters() 立即刷新。
默认值
| 属性 | 默认值 |
|---|---|
circleColorStops | 三段:0→#42A5F5、10→#FF9800、50→#F44336 |
circleRadius | 16f |
circleRadiusStops | null(使用单一 circleRadius) |
circleStrokeColor | Color.WHITE |
circleStrokeWidth | 2f |
circleOpacity | 0.85f |
textSize | 13f |
textColor | Color.WHITE |
textHaloColor | #33000000 |
textHaloWidth | 2f |
textFont | {"ftfont1 Medium"} |
singleIconBitmap | null(使用默认蓝色圆点) |
singleIconSize | 1f |
singleMarkerOptions | null |
链式方法
| 方法签名 | 说明 |
|---|---|
ClusterStyleOptions circleColorStops(List<ClusterColorStop> stops) | 设置聚合圆圈颜色分段,按 point_count 升序,第一段 threshold 应为 0;传 null 或空列表则使用默认蓝色 |
ClusterStyleOptions circleColor(int color) | 便捷设置聚合圆圈为单色(清空分段,仅保留 threshold=0 的该颜色) |
ClusterStyleOptions circleRadius(float radius) | 设置聚合圆圈单一半径(px) |
ClusterStyleOptions circleRadiusStops(List<ClusterRadiusStop> stops) | 设置聚合圆圈半径分段(按 point_count 升序,第一段 threshold 应为 0);传 null 或空时用单一 circleRadius |
ClusterStyleOptions circleStrokeColor(int color) | 设置聚合圆圈描边颜色 |
ClusterStyleOptions circleStrokeWidth(float width) | 设置聚合圆圈描边宽度 |
ClusterStyleOptions circleOpacity(float opacity) | 设置聚合圆圈透明度 |
ClusterStyleOptions textSize(float size) | 设置聚合数字字号 |
ClusterStyleOptions textColor(int color) | 设置聚合数字颜色 |
ClusterStyleOptions textHaloColor(int color) | 设置聚合数字光晕颜色 |
ClusterStyleOptions textHaloWidth(float width) | 设置聚合数字光晕宽度 |
ClusterStyleOptions textFont(String[] font) | 设置聚合数字字体;传 null 回退默认 {"ftfont1 Medium"} |
ClusterStyleOptions singleIconBitmap(Bitmap bitmap) | 设置单点图标 Bitmap;传 null 回退默认蓝色圆点 |
ClusterStyleOptions singleIconSize(float size) | 设置单点图标大小倍率(自定义图标时生效) |
ClusterStyleOptions singleMarkerOptions(MarkerOptions options) | 设置单点样式模板(MarkerOptions),从中提取 icon/anchor/rotateAngle 应用到单点 SymbolLayer,优先级高于 singleIconBitmap/singleIconSize |
Getter 方法
| 方法签名 | 说明 |
|---|---|
List<ClusterColorStop> getCircleColorStops() | 返回聚合圆圈颜色分段列表 |
float getCircleRadius() | 返回聚合圆圈单一半径 |
List<ClusterRadiusStop> getCircleRadiusStops() | 返回聚合圆圈半径分段列表 |
int getCircleStrokeColor() | 返回聚合圆圈描边颜色 |
float getCircleStrokeWidth() | 返回聚合圆圈描边宽度 |
float getCircleOpacity() | 返回聚合圆圈透明度 |
float getTextSize() | 返回聚合数字字号 |
int getTextColor() | 返回聚合数字颜色 |
int getTextHaloColor() | 返回聚合数字光晕颜色 |
float getTextHaloWidth() | 返回聚合数字光晕宽度 |
String[] getTextFont() | 返回聚合数字字体 |
Bitmap getSingleIconBitmap() | 返回单点图标 Bitmap |
float getSingleIconSize() | 返回单点图标大小倍率 |
MarkerOptions getSingleMarkerOptions() | 返回单点样式模板 |
单点图标优先级
recluster() 中单点图标的选取优先级(高 → 低):
singleMarkerOptions的icon(同时取其anchorU/anchorV作为锚点)singleIconBitmap(使用singleIconSize倍率)BitmapDescriptorFactory.defaultMarker(MARKER_COLOR_BLUE)(按屏幕密度归一化到约 36dp 宽)- 内置绘制的小蓝色圆点(24px,蓝色填充 + 白色描边)
Cluster
com.sfmap.api.maps.cluster.Cluster
聚合点集合,包含一组 ClusterItem。
| 方法签名 | 说明 |
|---|---|
int getClusterCount() | 返回聚合点数量 |
LatLng getCenterLatLng() | 返回聚合点中心经纬度(所有点的算术平均);集合为空时返回 null |
List<ClusterItem> getClusterItems() | 返回聚合点列表 |
void addClusterItem(ClusterItem item) | 内部使用:添加聚合项;item 为 null 时忽略 |
ClusterClickListener
com.sfmap.api.maps.cluster.ClusterClickListener
聚合点点击监听器接口。
| 方法签名 | 说明 |
|---|---|
void onClick(Marker marker, List<ClusterItem> clusterItems, int pointCount, LatLng clickPos) | 点击聚合点时回调。marker 为被点击的聚合 Marker(可能为 null);clusterItems 为该聚合包含的子项列表;pointCount 为命中聚合点包含的子项数量(单点为 1),-1 表示未知;clickPos 为点击的经纬度位置 |
ClusterRender
com.sfmap.api.maps.cluster.ClusterRender
聚合渲染器接口,由业务方实现,提供聚合 Marker 的样式。
| 方法签名 | 说明 |
|---|---|
MarkerOptions getClusterMarkerOptions(Cluster cluster) | 返回指定聚合点集合的 Marker 样式选项 |
ClusterColorStop
com.sfmap.api.maps.cluster.ClusterColorStop
聚合圆圈颜色分段项:当聚合点数量(point_count)达到 threshold 时使用对应 color。用于 ClusterStyleOptions 的聚合圆圈颜色按聚合数量分段着色,threshold 表示 point_count 的下限(含)。
构造方法
| 方法签名 | 说明 |
|---|---|
ClusterColorStop(int threshold, int color) | 构造一个颜色分段项,threshold 为聚合点数量下限(含),color 为命中该分段时使用的颜色 |
方法
| 方法签名 | 说明 |
|---|---|
int getThreshold() | 返回聚合点数量下限(含),point_count >= threshold 时命中 |
int getColor() | 返回命中该分段时使用的颜色 |
ClusterRadiusStop
com.sfmap.api.maps.cluster.ClusterRadiusStop
聚合圆圈半径分段项:当聚合点数量(point_count)达到 threshold 时使用对应 radius。与 ClusterColorStop 用法一致,用于 ClusterStyleOptions 的聚合圆圈半径按聚合数量分段。
构造方法
| 方法签名 | 说明 |
|---|---|
ClusterRadiusStop(int threshold, float radius) | 构造一个半径分段项,threshold 为聚合点数量下限(含),radius 为命中该分段时使用的半径(px) |
方法
| 方法签名 | 说明 |
|---|---|
int getThreshold() | 返回聚合点数量下限(含),point_count >= threshold 时命中 |
float getRadius() | 返回命中该分段时使用的半径(px) |
