diff --git a/example/lib/pages/map_controller.dart b/example/lib/pages/map_controller.dart index 5534470ed..ba622c16b 100644 --- a/example/lib/pages/map_controller.dart +++ b/example/lib/pages/map_controller.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong/latlong.dart'; +import 'package:location/location.dart'; import '../widgets/drawer.dart'; @@ -92,6 +93,7 @@ class MapControllerPageState extends State { mapController.move(dublin, 5.0); }, ), + CurrentLocation(mapController: mapController), ], ), ), @@ -171,3 +173,49 @@ class MapControllerPageState extends State { ); } } + +class CurrentLocation extends StatefulWidget { + const CurrentLocation({ + Key key, + @required this.mapController, + }) : super(key: key); + + final MapController mapController; + + @override + _CurrentLocationState createState() => _CurrentLocationState(); +} + +class _CurrentLocationState extends State { + var icon = Icons.gps_not_fixed; + + void _moveToCurrent() async { + var location = Location(); + + try { + var currentLocation = await location.getLocation(); + widget.mapController.move( + LatLng(currentLocation.latitude, currentLocation.longitude), 18); + + setState(() { + icon = Icons.gps_fixed; + }); + await widget.mapController.position.first; + setState(() { + icon = Icons.gps_not_fixed; + }); + } catch (e) { + setState(() { + icon = Icons.gps_off; + }); + } + } + + @override + Widget build(BuildContext context) { + return IconButton( + icon: Icon(icon), + onPressed: _moveToCurrent, + ); + } +} diff --git a/lib/flutter_map.dart b/lib/flutter_map.dart index 2b90badbe..cc2254036 100644 --- a/lib/flutter_map.dart +++ b/lib/flutter_map.dart @@ -91,6 +91,8 @@ abstract class MapController { ValueChanged onRotationChanged; + Stream get position; + factory MapController() => MapControllerImpl(); } diff --git a/lib/src/map/map.dart b/lib/src/map/map.dart index 1a4334c76..7ba9f6e97 100644 --- a/lib/src/map/map.dart +++ b/lib/src/map/map.dart @@ -57,11 +57,15 @@ class MapControllerImpl implements MapController { @override ValueChanged onRotationChanged; + + @override + Stream get position => _state._positionSink.stream; } class MapState { MapOptions options; final StreamController _onMoveSink; + final StreamController _positionSink; double _zoom; double rotation; @@ -77,7 +81,8 @@ class MapState { MapState(this.options) : rotation = options.rotation, _zoom = options.zoom, - _onMoveSink = StreamController.broadcast(); + _onMoveSink = StreamController.broadcast(), + _positionSink = StreamController.broadcast(); CustomPoint _size; @@ -131,21 +136,19 @@ class MapState { center = options.containPoint(center, _lastCenter ?? center); } + var mapPosition = MapPosition( + center: center, bounds: bounds, zoom: zoom, hasGesture: hasGesture); + _zoom = zoom; _lastCenter = center; _lastPixelBounds = getPixelBounds(_zoom); _lastBounds = _calculateBounds(); _pixelOrigin = getNewPixelOrigin(center); _onMoveSink.add(null); + _positionSink.add(mapPosition); if (options.onPositionChanged != null) { - options.onPositionChanged( - MapPosition( - center: center, - bounds: bounds, - zoom: zoom, - ), - hasGesture); + options.onPositionChanged(mapPosition, hasGesture); } }