flutter_map_marker_cluster 0.2.5 flutter_map_marker_cluster: ^0.2.5 copied to clipboard
A Dart implementation of Leaflet.makercluster for Flutter apps. Provides beautiful animated marker clustering functionality for flutter_map.
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:latlong/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> {
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(
child: Icon(Icons.refresh),
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),
);
// one of this
markers = List.from(markers);
// markers = [...markers];
// markers = []..addAll(markers);
});
},
),
body: FlutterMap(
options: MapOptions(
center: points[0],
zoom: 5,
plugins: [
MarkerClusterPlugin(),
],
),
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),
builder: (context, markers) {
return FloatingActionButton(
child: Text(markers.length.toString()),
onPressed: null,
);
},
),
],
),
);
}
}