Add location permissions and implement real-time location updates on the map

This commit is contained in:
Van Leemput Dayron
2025-10-05 23:35:56 +02:00
parent da39e0e164
commit 4a38ec720f
7 changed files with 156 additions and 13 deletions

View File

@@ -1,5 +1,8 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
class MapContent extends StatefulWidget {
const MapContent({super.key});
@@ -9,7 +12,18 @@ class MapContent extends StatefulWidget {
}
class _MapContentState extends State<MapContent> {
static LatLng _pGooglePlex = LatLng(50.64864814740268, 3.7224066213297293);
Location locationController = Location();
final Completer<GoogleMapController> _mapController =
Completer<GoogleMapController>();
LatLng? currentPosition;
@override
void initState() {
super.initState();
getLocationUpdate();
}
@override
Widget build(BuildContext context) {
@@ -21,7 +35,7 @@ class _MapContentState extends State<MapContent> {
children: [
TextField(
decoration: InputDecoration(
hintText: 'Rechercher un lieu',
hintText: 'Rechercher un lieu...',
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
@@ -29,25 +43,91 @@ class _MapContentState extends State<MapContent> {
),
),
SizedBox(height: 8.0),
Expanded(
child: GoogleMap(
initialCameraPosition: CameraPosition(
target: _pGooglePlex,
zoom: 14.4746,
),
markers: {
Marker(
markerId: MarkerId('googlePlex'),
position: _pGooglePlex,
infoWindow: InfoWindow(title: 'Google Plex'),
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
onPressed: () {
// TODO : Logique de recherche à implémenter
},
child: Text('Chercher'),
),
),
SizedBox(height: 8.0),
Expanded(
child: currentPosition == null
? const Center(child: Text("Chargement..."))
: GoogleMap(
onMapCreated: (GoogleMapController controller) =>
_mapController.complete(controller),
initialCameraPosition: CameraPosition(
target: currentPosition!,
zoom: 14.4746,
),
markers: {
Marker(
markerId: MarkerId('currentLocation'),
position: currentPosition!,
icon: BitmapDescriptor.defaultMarkerWithHue(
BitmapDescriptor.hueAzure,
),
),
},
),
),
],
),
),
),
);
}
Future<void> _cameraToPosition(LatLng pos) async {
final GoogleMapController controller = await _mapController.future;
CameraPosition newCameraPosition = CameraPosition(target: pos, zoom: 13);
await controller.animateCamera(
CameraUpdate.newCameraPosition(newCameraPosition),
);
}
Future<void> getLocationUpdate() async {
bool serviceEnabled;
PermissionStatus permissionGranted;
serviceEnabled = await locationController.serviceEnabled();
if (serviceEnabled) {
serviceEnabled = await locationController.requestService();
} else {
return;
}
permissionGranted = await locationController.hasPermission();
if (permissionGranted == PermissionStatus.denied) {
permissionGranted = await locationController.requestPermission();
if (permissionGranted != PermissionStatus.granted) {
return;
}
}
locationController.onLocationChanged.listen((LocationData currentLocation) {
if (currentLocation.latitude != null &&
currentLocation.longitude != null) {
setState(() {
currentPosition = LatLng(
currentLocation.latitude!,
currentLocation.longitude!,
);
_cameraToPosition(currentPosition!);
print(currentPosition);
});
}
});
}
}