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}); @override State createState() => _MapContentState(); } class _MapContentState extends State { Location locationController = Location(); final Completer _mapController = Completer(); LatLng? currentPosition; @override void initState() { super.initState(); //getLocationUpdate(); currentPosition = LatLng(48.8566, 2.3522); // Position par défaut (Paris) } @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: Padding( padding: EdgeInsets.all(8.0), child: Column( children: [ TextField( decoration: InputDecoration( hintText: 'Rechercher un lieu...', prefixIcon: Icon(Icons.search), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), ), ), ), SizedBox(height: 8.0), 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: CircularProgressIndicator(), ) : GoogleMap( onMapCreated: (GoogleMapController controller) => _mapController.complete(controller), initialCameraPosition: CameraPosition( target: currentPosition!, zoom: 14.4746, ), myLocationEnabled: true, myLocationButtonEnabled: true, mapType: MapType.normal, zoomControlsEnabled: true, markers: { Marker( markerId: MarkerId('currentLocation'), position: currentPosition!, icon: BitmapDescriptor.defaultMarkerWithHue( BitmapDescriptor.hueAzure, ), ), }, ), ), ], ), ), ), ); } Future _cameraToPosition(LatLng pos) async { final GoogleMapController controller = await _mapController.future; CameraPosition newCameraPosition = CameraPosition(target: pos, zoom: 13); await controller.animateCamera( CameraUpdate.newCameraPosition(newCameraPosition), ); } Future 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); }); } }); } }