Fix retrieving location not working on android
This commit is contained in:
parent
32793f2129
commit
275416ea3c
@ -270,21 +270,29 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _getCurrentLocation() async {
|
Future<void> _getCurrentLocation() async {
|
||||||
LocationPermission permission = await Geolocator.checkPermission();
|
LocationPermission? permission;
|
||||||
if(permission != LocationPermission.always || permission != LocationPermission.whileInUse){
|
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);
|
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
|
||||||
_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);
|
||||||
});
|
});
|
||||||
|
|
||||||
LatLngBounds bounds = _mapController.camera.visibleBounds;
|
LatLngBounds bounds = _mapController.camera.visibleBounds;
|
||||||
|
|
||||||
_getOpenStreetMapData(LatLng(bounds.southWest.latitude, bounds.southWest.longitude),LatLng(bounds.northEast.latitude, bounds.northEast.longitude));
|
_getOpenStreetMapData(LatLng(bounds.southWest.latitude, bounds.southWest.longitude),LatLng(bounds.northEast.latitude, bounds.northEast.longitude));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _getOpenStreetMapData(LatLng southWest, LatLng northEast) async {
|
Future<void> _getOpenStreetMapData(LatLng southWest, LatLng northEast) async {
|
||||||
|
Loading…
Reference in New Issue
Block a user