From 48c785c5865b43b85126f998efd313069f5e2ca3 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 27 Dec 2024 23:31:49 +0100 Subject: [PATCH] change mapbox place to google place api --- covas_mobile/lib/pages/EditEvent.dart | 29 ++++++++++---------- covas_mobile/lib/pages/UpdateEventImage.dart | 25 ++++++++--------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/covas_mobile/lib/pages/EditEvent.dart b/covas_mobile/lib/pages/EditEvent.dart index fedc2bb..3f95af3 100644 --- a/covas_mobile/lib/pages/EditEvent.dart +++ b/covas_mobile/lib/pages/EditEvent.dart @@ -180,24 +180,23 @@ class _EditEventState extends State if (accessToken.isNotEmpty) { try { await dotenv.load(); - final mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? ''; - print("place non encoded : ${place}"); - final url = - 'https://api.mapbox.com/geocoding/v5/mapbox.places/${place}.json?access_token=${mapboxAccessToken}&types=poi,address,place'; - var encoded = Uri.encodeFull(url); - print("encoded : ${encoded}"); - final response = await http.get(Uri.parse(encoded)); + final ApiTokenGoogle = dotenv.env['PLACE_API_KEY'] ?? ''; + // Searchbox API for geocoding the place (No session token) + final searchboxUrl = Uri.parse( + 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=${place}&key=${ApiTokenGoogle}'); - if (response.statusCode == 200) { - final data = json.decode(response.body); + // Perform the request + final searchboxResponse = await http.get(searchboxUrl); + + if (searchboxResponse.statusCode == 200) { + final data = json.decode(searchboxResponse.body); print("data : ${data}"); - if (data['features'].isNotEmpty) { - place = data['features'][0]['place_name']; - final coordinates = - data['features'][0]['geometry']['coordinates']; - final longitude = coordinates[0]; // Longitude - final latitude = coordinates[1]; // Latitude + if (data['results'].isNotEmpty) { + place = data['results'][0]['formatted_address']; + final coordinates = data['results'][0]['geometry']['location']; + final longitude = coordinates["lng"]; // Longitude + final latitude = coordinates["lat"]; // Latitude var urlGet = Uri.parse( "${globals.api}/events/search?item=${name}&date_event=${startDate}&min_lat=$latitude&max_lat=$latitude" "&min_lon=$longitude&max_lon=$longitude"); diff --git a/covas_mobile/lib/pages/UpdateEventImage.dart b/covas_mobile/lib/pages/UpdateEventImage.dart index df19763..83a1c85 100644 --- a/covas_mobile/lib/pages/UpdateEventImage.dart +++ b/covas_mobile/lib/pages/UpdateEventImage.dart @@ -170,12 +170,12 @@ class _UpdateeventImageState extends State try { await dotenv.load(); - final mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? ''; - + final ApiTokenGoogle = dotenv.env['PLACE_API_KEY'] ?? ''; // Searchbox API for geocoding the place (No session token) final searchboxUrl = Uri.parse( - 'https://api.mapbox.com/search/geocode/v6/forward?access_token=${mapboxAccessToken}&q=${place}'); + 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=${place}&key=${ApiTokenGoogle}'); + // Perform the request final searchboxResponse = await http.get(searchboxUrl); if (searchboxResponse.statusCode != 200) { @@ -184,17 +184,17 @@ class _UpdateeventImageState extends State } final searchboxData = json.decode(searchboxResponse.body); - if (searchboxData['features'].isEmpty) { + if (searchboxData['results'].isEmpty) { showErrorDialog(context, "Lieu introuvable"); return; } // Extract place details from the searchbox response - final firstFeature = searchboxData['features'][0]; - place = firstFeature['properties']["full_address"]; - final coordinates = firstFeature['geometry']['coordinates']; - final longitude = coordinates[0]; - final latitude = coordinates[1]; + final firstFeature = searchboxData['results'][0]; + place = firstFeature["formatted_address"]; + final coordinates = firstFeature['geometry']['location']; + final longitude = coordinates["lng"]; + final latitude = coordinates["lat"]; // Check if a similar event exists final eventsUrl = Uri.parse( @@ -315,11 +315,9 @@ class _UpdateeventImageState extends State } Future searchSuggestions(String input) async { - - String sessionToken = uuid.v4(); await dotenv.load(fileName: ".env"); // Load .env file - final ApiTokenGoogle = dotenv.env['GEMINI_API_KEY'] ?? ''; + final ApiTokenGoogle = dotenv.env['PLACE_API_KEY'] ?? ''; // Define the Searchbox API URL final searchboxUrl = Uri.parse( @@ -395,7 +393,8 @@ class _UpdateeventImageState extends State print("suggestion tapped : ${suggestions[index]}"); setState(() { - geographicalZone = suggestions[index]['formatted_address']; + geographicalZone = + suggestions[index]['formatted_address']; inputGeo.text = geographicalZone; suggestions.clear(); });