when you get your current location, it will show attractions in the area

This commit is contained in:
LilleBRG 2024-09-03 14:05:18 +02:00
parent d58df6e529
commit b213523911

View File

@ -218,17 +218,25 @@ class _MyHomePageState extends State<MyHomePage> {
Future<void> GetOpenStreetMapArea() async { Future<void> _getOpenStreetMapArea(LatLng fromGetLocation) async {
final dynamic location; final dynamic location;
if (searchBarInput.text == '' && fromGetLocation == const LatLng(00.0000, 00.0000)) {
if (searchBarInput.text != '') { return;
}
_searchResults.clear(); final http.Response response;
_searchResults.clear();
final response = await http.get( if (fromGetLocation == const LatLng(00.0000, 00.0000)) {
response = await http.get(
Uri.parse('https://nominatim.openstreetmap.org/search.php?q=Attractions+in+${searchBarInput.text}&format=jsonv2'), Uri.parse('https://nominatim.openstreetmap.org/search.php?q=Attractions+in+${searchBarInput.text}&format=jsonv2'),
headers: {'User-Agent': 'SkanTravels/1.0'}, headers: {'User-Agent': 'SkanTravels/1.0'},
); );
}
else{
response = await http.get(
Uri.parse('https://nominatim.openstreetmap.org/search.php?q=Attractions+near+${fromGetLocation.latitude}%2C+${fromGetLocation.longitude}&format=jsonv2'),
headers: {'User-Agent': 'SkanTravels/1.0'},
);
}
location = jsonDecode(response.body); location = jsonDecode(response.body);
@ -251,21 +259,29 @@ class _MyHomePageState extends State<MyHomePage> {
} }
} }
_mapController.move(_searchResults[0].location, 9); if(fromGetLocation == const LatLng(00.0000, 00.0000)){
_mapController.move(_searchResults[0].location, 9);
}
} }
}
} }
Future<void> _getCurrentLocation() async { Future<void> _getCurrentLocation() async {
LocationPermission permission = await Geolocator.checkPermission(); LocationPermission permission = await Geolocator.checkPermission();
if(permission == LocationPermission.always ) if(permission != LocationPermission.always || permission != LocationPermission.whileInUse){
await Geolocator.requestPermission(); }
else{
await Geolocator.requestPermission();
}
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
_getOpenStreetMapArea(LatLng(position.latitude, position.longitude));
_mapController.move(LatLng(position.latitude, position.longitude), 10); _mapController.move(LatLng(position.latitude, position.longitude), 10);
setState(() { setState(() {
_userPosition = LatLng(position.latitude, position.longitude); _userPosition = LatLng(position.latitude, position.longitude);
}); });
} }
@ -388,7 +404,9 @@ class _MyHomePageState extends State<MyHomePage> {
width: 100, width: 100,
height: 28, height: 28,
child: ElevatedButton( child: ElevatedButton(
onPressed: GetOpenStreetMapArea, onPressed: () {
_getOpenStreetMapArea(const LatLng(00.0000, 00.0000));
},
child: const Text('Search'), child: const Text('Search'),
), ),
), ),