import'dart:async';
import'package:flutter/material.dart';
import'package:flutter_map/flutter_map.dart';
import'package:latlong/latlong.dart';
import'package:map_controller/map_controller.dart';
class_MapPageStateextendsState<MapPage> {
MapController mapController;
StatefulMapController statefulMapController;
StreamSubscription<StatefulMapControllerStateChange> sub;
@overridevoidinitState() {
// intialize the controllers
mapController = MapController();
statefulMapController = StatefulMapController(mapController: mapController);
// wait for the controller to be ready before using it
statefulMapController.onReady.then((_) => print("The map controller is ready")));
/// [Important] listen to the changefeed to rebuild the map on changes:/// this will rebuild the map when for example addMarker or any method /// that mutates the map assets is called
sub = statefulMapController.changeFeed.listen((change) => setState(() {}));
super.initState();
}
@overrideWidgetbuild(BuildContext context) {
returnScaffold(
body: SafeArea(
child: Stack(children: <Widget>[
FlutterMap(
mapController: mapController,
options: MapOptions(center: LatLng(48.853831, 2.348722), zoom: 11.0),
layers: [
MarkerLayerOptions(markers: statefulMapController.markers),
PolylineLayerOptions(polylines: statefulMapController.lines),
PolygonLayerOptions(polygons: statefulMapController.polygons)
],
),
// ...
])),
);
}
@overridevoiddispose() {
sub.cancel();
super.dispose();
}
}
classMapPageextendsStatefulWidget{
@override_MapPageStatecreateState() => _MapPageState();
}