创建 ClusterManager
和地图:
使用 MarkerClusterLayerOptions
来配置聚合标记层,并将其添加到 flutter_map
中。
class MyMap extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FlutterMap(
options: MapOptions(
center: LatLng(51.5, -0.09),
zoom: 10.0,
),
layers: [
TileLayerOptions(
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c'],
),
MarkerClusterLayerOptions(
maxClusterRadius: 120,
size: Size(40, 40),
anchors: Anchors(
anchorPos: AnchorPos.align(AnchorAlign.center),
),
markers: [
Marker(
width: 40.0,
height: 40.0,
point: LatLng(51.5, -0.09),
builder: (ctx) => Container(
child: Icon(Icons.location_on),
),
),
// 添加更多标记...
],
builder: (context, markers) {
return Container(
decoration: BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
),
child: Center(
child: Text(
markers.length.toString(),
style: TextStyle(color: Colors.white),
),
),
);
},
),
],
);
}
}