diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index f374bbc..8c9099c 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,6 @@ + + UIApplicationSupportsIndirectInputEvents + NSLocationWhenInUseUsageDescription + We need your location to show it on the map. + NSLocationAlwaysAndWhenInUseUsageDescription + We need your location to show it on the map even in background. diff --git a/lib/components/map/map_content.dart b/lib/components/map/map_content.dart index 37c7aed..5b20c51 100644 --- a/lib/components/map/map_content.dart +++ b/lib/components/map/map_content.dart @@ -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 { - static LatLng _pGooglePlex = LatLng(50.64864814740268, 3.7224066213297293); + Location locationController = Location(); + + final Completer _mapController = + Completer(); + + LatLng? currentPosition; + + @override + void initState() { + super.initState(); + getLocationUpdate(); + } @override Widget build(BuildContext context) { @@ -21,7 +35,7 @@ class _MapContentState extends State { 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 { ), ), 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 _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); + }); + } + }); + } } diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index b8e2b22..93e0730 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,10 +5,12 @@ import FlutterMacOS import Foundation +import location import path_provider_foundation import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + LocationPlugin.register(with: registry.registrar(forPlugin: "LocationPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index a3c52f0..22e2dbf 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -176,6 +176,22 @@ packages: url: "https://pub.dev" source: hosted version: "0.15.6" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571" + url: "https://pub.dev" + source: hosted + version: "4.1.2" + js: + dependency: transitive + description: + name: js + sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3 + url: "https://pub.dev" + source: hosted + version: "0.6.7" leak_tracker: dependency: transitive description: @@ -208,6 +224,30 @@ packages: url: "https://pub.dev" source: hosted version: "5.1.1" + location: + dependency: "direct main" + description: + name: location + sha256: "06be54f682c9073cbfec3899eb9bc8ed90faa0e17735c9d9fa7fe426f5be1dd1" + url: "https://pub.dev" + source: hosted + version: "5.0.3" + location_platform_interface: + dependency: transitive + description: + name: location_platform_interface + sha256: "8aa1d34eeecc979d7c9fe372931d84f6d2ebbd52226a54fe1620de6fdc0753b1" + url: "https://pub.dev" + source: hosted + version: "3.1.2" + location_web: + dependency: transitive + description: + name: location_web + sha256: ec484c66e8a4ff1ee5d044c203f4b6b71e3a0556a97b739a5bc9616de672412b + url: "https://pub.dev" + source: hosted + version: "4.2.0" matcher: dependency: transitive description: @@ -445,6 +485,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.6" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 + url: "https://pub.dev" + source: hosted + version: "1.4.0" vector_math: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5c15b36..8da6ae8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,6 +34,7 @@ dependencies: shared_preferences: ^2.2.2 path_provider: ^2.1.1 bcrypt: ^1.1.3 + location: ^5.0.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.