Display user location on map

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

View File

@ -47,8 +47,9 @@ android {
applicationId "edu.furman.now" applicationId "edu.furman.now"
// You can update the following values to match your application needs. // You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
minSdkVersion flutter.minSdkVersion minSdkVersion 23
targetSdkVersion flutter.targetSdkVersion targetSdkVersion flutter.targetSdkVersion
compileSdkVersion 33
versionCode flutterVersionCode.toInteger() versionCode flutterVersionCode.toInteger()
versionName flutterVersionName versionName flutterVersionName
} }

View File

@ -34,4 +34,5 @@
<!-- Permissions --> <!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest> </manifest>

View File

@ -1,5 +1,8 @@
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.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:flutter_svg/svg.dart';
import 'package:furman_now/src/utils/theme.dart'; import 'package:furman_now/src/utils/theme.dart';
import 'package:furman_now/src/widgets/map/filter_chip.dart'; import 'package:furman_now/src/widgets/map/filter_chip.dart';
@ -22,6 +25,9 @@ class _MapScreenState extends State<MapScreen>
vsync: this, vsync: this,
); );
late CenterOnLocationUpdate _centerOnLocationUpdate;
late StreamController<double?> _centerCurrentLocationStreamController;
var _rotation = 0.0; var _rotation = 0.0;
@override @override
@ -34,6 +40,8 @@ class _MapScreenState extends State<MapScreen>
}); });
} }
}); });
_centerOnLocationUpdate = CenterOnLocationUpdate.always;
_centerCurrentLocationStreamController = StreamController<double?>();
} }
@override @override
@ -89,6 +97,20 @@ class _MapScreenState extends State<MapScreen>
zoom: 15, zoom: 15,
minZoom: 12, minZoom: 12,
maxZoom: 18, maxZoom: 18,
plugins: [
LocationMarkerPlugin(
centerCurrentLocationStream:
_centerCurrentLocationStreamController.stream,
centerOnLocationUpdate: _centerOnLocationUpdate,
),
],
onPositionChanged: (MapPosition position, bool hasGesture) {
if (hasGesture) {
setState(
() => _centerOnLocationUpdate = CenterOnLocationUpdate.never,
);
}
},
), ),
layers: [ layers: [
TileLayerOptions( TileLayerOptions(
@ -96,6 +118,7 @@ class _MapScreenState extends State<MapScreen>
"https://tile.openstreetmap.org/{z}/{x}/{y}.png", "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
userAgentPackageName: 'edu.furman.now', userAgentPackageName: 'edu.furman.now',
), ),
LocationMarkerLayerOptions(),
], ],
nonRotatedChildren: [ nonRotatedChildren: [
AttributionWidget( AttributionWidget(
@ -111,6 +134,7 @@ class _MapScreenState extends State<MapScreen>
), ),
], ],
), ),
// Rotation reset fab
Positioned( Positioned(
top: 12, top: 12,
left: 0, 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,
),
)
)
], ],
), ),
), ),

View File

@ -146,6 +146,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_compass:
dependency: transitive
description:
name: flutter_compass
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.0"
flutter_lints: flutter_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:
@ -160,6 +167,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.2.0" version: "2.2.0"
flutter_map_location_marker:
dependency: "direct main"
description:
name: flutter_map_location_marker
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.4"
flutter_svg: flutter_svg:
dependency: "direct main" dependency: "direct main"
description: description:
@ -172,6 +186,11 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
frontend_server_client: frontend_server_client:
dependency: transitive dependency: transitive
description: description:
@ -179,6 +198,48 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.3" version: "2.1.3"
geolocator:
dependency: transitive
description:
name: geolocator
url: "https://pub.dartlang.org"
source: hosted
version: "9.0.1"
geolocator_android:
dependency: transitive
description:
name: geolocator_android
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.0"
geolocator_apple:
dependency: transitive
description:
name: geolocator_apple
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
geolocator_platform_interface:
dependency: transitive
description:
name: geolocator_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.6"
geolocator_web:
dependency: transitive
description:
name: geolocator_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.6"
geolocator_windows:
dependency: transitive
description:
name: geolocator_windows
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1"
glob: glob:
dependency: transitive dependency: transitive
description: description:

View File

@ -48,6 +48,7 @@ dependencies:
palette_generator: ^0.3.3+2 palette_generator: ^0.3.3+2
flutter_svg: ^1.1.4 flutter_svg: ^1.1.4
auto_route: ^5.0.1 auto_route: ^5.0.1
flutter_map_location_marker: ^4.1.4
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: