Display user location on map

This commit is contained in:
2022-09-01 13:48:33 -04:00
parent a8e802ffab
commit 17350d798d
5 changed files with 107 additions and 1 deletions

View File

@@ -1,5 +1,8 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:flutter_svg/svg.dart';
import 'package:furman_now/src/utils/theme.dart';
import 'package:furman_now/src/widgets/map/filter_chip.dart';
@@ -22,6 +25,9 @@ class _MapScreenState extends State<MapScreen>
vsync: this,
);
late CenterOnLocationUpdate _centerOnLocationUpdate;
late StreamController<double?> _centerCurrentLocationStreamController;
var _rotation = 0.0;
@override
@@ -34,6 +40,8 @@ class _MapScreenState extends State<MapScreen>
});
}
});
_centerOnLocationUpdate = CenterOnLocationUpdate.always;
_centerCurrentLocationStreamController = StreamController<double?>();
}
@override
@@ -89,6 +97,20 @@ class _MapScreenState extends State<MapScreen>
zoom: 15,
minZoom: 12,
maxZoom: 18,
plugins: [
LocationMarkerPlugin(
centerCurrentLocationStream:
_centerCurrentLocationStreamController.stream,
centerOnLocationUpdate: _centerOnLocationUpdate,
),
],
onPositionChanged: (MapPosition position, bool hasGesture) {
if (hasGesture) {
setState(
() => _centerOnLocationUpdate = CenterOnLocationUpdate.never,
);
}
},
),
layers: [
TileLayerOptions(
@@ -96,6 +118,7 @@ class _MapScreenState extends State<MapScreen>
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
userAgentPackageName: 'edu.furman.now',
),
LocationMarkerLayerOptions(),
],
nonRotatedChildren: [
AttributionWidget(
@@ -111,6 +134,7 @@ class _MapScreenState extends State<MapScreen>
),
],
),
// Rotation reset fab
Positioned(
top: 12,
left: 0,
@@ -177,6 +201,24 @@ class _MapScreenState extends State<MapScreen>
),
),
),
Positioned(
right: 20,
bottom: 20,
child: FloatingActionButton(
onPressed: () {
// Automatically center the location marker on the map when location updated until user interact with the map.
setState(
() => _centerOnLocationUpdate = CenterOnLocationUpdate.always,
);
// Center the location marker on the map and zoom the map to level 18.
_centerCurrentLocationStreamController.add(16);
},
child: const Icon(
Icons.my_location,
color: Colors.white,
),
)
)
],
),
),