flutter_map_marker_cluster 0.4.0 flutter_map_marker_cluster: ^0.4.0 copied to clipboard
A Dart implementation of Leaflet.makercluster for Flutter apps. Provides beautiful animated marker clustering functionality for flutter_map.
// @dart=2.9
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_marker_cluster/flutter_map_marker_cluster.dart';
import 'package:latlong2/latlong.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final PopupController _popupController = PopupController();
List<Marker> markers;
int pointIndex;
List points = [
LatLng(51.5, -0.09),
LatLng(49.8566, 3.3522),
];
@override
void initState() {
pointIndex = 0;
markers = [
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: points[pointIndex],
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(53.3498, -6.2603),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(53.3488, -6.2613),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(53.3488, -6.2613),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(48.8566, 2.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
Marker(
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
point: LatLng(49.8566, 3.3522),
builder: (ctx) => Icon(Icons.pin_drop),
),
];
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
pointIndex++;
if (pointIndex >= points.length) {
pointIndex = 0;
}
setState(() {
markers[0] = Marker(
point: points[pointIndex],
anchorPos: AnchorPos.align(AnchorAlign.center),
height: 30,
width: 30,
builder: (ctx) => Icon(Icons.pin_drop),
);
markers = List.from(markers);
});
},
child: Icon(Icons.refresh),
),
body: FlutterMap(
options: MapOptions(
center: points[0],
zoom: 5,
maxZoom: 15,
plugins: [
MarkerClusterPlugin(),
],
onTap: (_) => _popupController
.hidePopup(), // Hide popup when the map is tapped.
),
layers: [
TileLayerOptions(
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c'],
),
MarkerClusterLayerOptions(
maxClusterRadius: 120,
size: Size(40, 40),
anchor: AnchorPos.align(AnchorAlign.center),
fitBoundsOptions: FitBoundsOptions(
padding: EdgeInsets.all(50),
),
markers: markers,
polygonOptions: PolygonOptions(
borderColor: Colors.blueAccent,
color: Colors.black12,
borderStrokeWidth: 3),
popupOptions: PopupOptions(
popupSnap: PopupSnap.markerTop,
popupController: _popupController,
popupBuilder: (_, marker) => Container(
width: 200,
height: 100,
color: Colors.white,
child: GestureDetector(
onTap: () => debugPrint('Popup tap!'),
child: Text(
'Container popup for marker at ${marker.point}',
),
),
)),
builder: (context, markers) {
return FloatingActionButton(
onPressed: null,
child: Text(markers.length.toString()),
);
},
),
],
),
);
}
}