diff --git a/Mobile/lib/main.dart b/Mobile/lib/main.dart index 9c5e11a..f035a73 100644 --- a/Mobile/lib/main.dart +++ b/Mobile/lib/main.dart @@ -249,43 +249,51 @@ class _MyHomePageState extends State { }); } - Future _onSearch() async { - final http.Response response = await http.get( - Uri.parse('https://nominatim.openstreetmap.org/search.php?q=${searchBarInput.text}&format=jsonv2'), - headers: {'User-Agent': 'SkanTravels/1.0'} - ); + Future _onSearch() async { + final http.Response response = await http.get( + Uri.parse('https://nominatim.openstreetmap.org/search.php?q=${searchBarInput.text}&format=jsonv2'), + headers: {'User-Agent': 'SkanTravels/1.0'} + ); - final dynamic location = jsonDecode(response.body); + final dynamic location = jsonDecode(response.body); - // Move the map to the center of the first search result - _mapController.move( - LatLng(double.parse(location[0]['lat']), double.parse(location[0]['lon'])), - 8 - ); + // Move the map to the center of the first search result + _mapController.move( + LatLng(double.parse(location[0]['lat']), double.parse(location[0]['lon'])), + 8 + ); - // Extract the bounding box and convert to LatLng - final List boundingBox = location[0]['boundingbox']; + // Extract the bounding box and convert to LatLng + final List boundingBox = location[0]['boundingbox']; - _getOpenStreetMapData(LatLng(double.parse(boundingBox[0]), double.parse(boundingBox[2])), LatLng(double.parse(boundingBox[1]), double.parse(boundingBox[3]))); -} + _getOpenStreetMapData(LatLng(double.parse(boundingBox[0]), double.parse(boundingBox[2])), LatLng(double.parse(boundingBox[1]), double.parse(boundingBox[3]))); + } - Future _getCurrentLocation() async { - LocationPermission permission = await Geolocator.checkPermission(); - if(permission != LocationPermission.always || permission != LocationPermission.whileInUse){ + Future _getCurrentLocation() async { + LocationPermission? permission; + try { + permission = await Geolocator.requestPermission(); + } catch (e) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error retrieving location: $e'))); + return; } - else{ - await Geolocator.requestPermission(); + + if (permission != LocationPermission.always && permission != LocationPermission.whileInUse) { + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Location permission denied'))); + return; } + Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); _mapController.move(LatLng(position.latitude, position.longitude), 10); + setState(() { _userPosition = LatLng(position.latitude, position.longitude); }); + LatLngBounds bounds = _mapController.camera.visibleBounds; _getOpenStreetMapData(LatLng(bounds.southWest.latitude, bounds.southWest.longitude),LatLng(bounds.northEast.latitude, bounds.northEast.longitude)); - - } + } Future _getOpenStreetMapData(LatLng southWest, LatLng northEast) async {