Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller position stream #505

Merged
merged 3 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions example/lib/pages/map_controller.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -92,6 +93,7 @@ class MapControllerPageState extends State<MapControllerPage> {
mapController.move(dublin, 5.0);
},
),
CurrentLocation(mapController: mapController),
],
),
),
Expand Down Expand Up @@ -171,3 +173,49 @@ class MapControllerPageState extends State<MapControllerPage> {
);
}
}

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<CurrentLocation> {
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,
);
}
}
2 changes: 2 additions & 0 deletions lib/flutter_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ abstract class MapController {

ValueChanged<double> onRotationChanged;

Stream<MapPosition> get position;

factory MapController() => MapControllerImpl();
}

Expand Down
19 changes: 11 additions & 8 deletions lib/src/map/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ class MapControllerImpl implements MapController {

@override
ValueChanged<double> onRotationChanged;

@override
Stream<MapPosition> get position => _state._positionSink.stream;
}

class MapState {
MapOptions options;
final StreamController<Null> _onMoveSink;
final StreamController<MapPosition> _positionSink;

double _zoom;
double rotation;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand Down