Reputation: 1838
i am trying to get user current location in flutter but its showing map, but not pointing my current location. I have added all required permissions in AndriodManifest file.
here is snap
here are the logs
I/Google Maps Android API(24140): Google Play services package version: 201817022
E/GoogleMapController(24140): Cannot enable MyLocation layer as location permissions are not granted
D/HostConnection(24140): HostConnection::get() New Host Connection established 0xefab0ec0, tid 25110
E/flutter (24140): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: User denied permissions to access the device's location.
E/flutter (24140): #0 MethodChannelGeolocator._handlePlatformException
package:geolocator_platform_interface/…/implementations/method_channel_geolocator.dart:242
E/flutter (24140): #1 MethodChannelGeolocator.getCurrentPosition
package:geolocator_platform_interface/…/implementations/method_channel_geolocator.dart:124
E/flutter (24140): <asynchronous suspension>
E/flutter (24140): #2 _locationState.locatepostion
package:map_app/abc.dart:26
E/flutter (24140): <asynchronous suspension>
W/System (24140): A resource failed to call release.
code
Completer<GoogleMapController> _controllerGoogleMap=Completer();
late GoogleMapController newGoogleMapController;
double mapbottompadding=0;
GlobalKey<ScaffoldState> scaffoldkey=new GlobalKey<ScaffoldState>();
late Position currentpositon;
var geolocator=Geolocator();
void locatepostion() async{
Position position=await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
currentpositon=position; // this is line 26, it is point before await
LatLng latLngPosition=LatLng(position.latitude,position.longitude);
CameraPosition cameraPosition=new CameraPosition(target: latLngPosition,zoom: 14);
newGoogleMapController.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
}
static final CameraPosition googlepostion=CameraPosition(target: LatLng(37.4249,-122.0657));
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(children: [
GoogleMap(
padding: EdgeInsets.only(bottom: mapbottompadding),
mapType: MapType.normal,
myLocationButtonEnabled: true,
myLocationEnabled: true,
zoomControlsEnabled: true,
zoomGesturesEnabled: true,
initialCameraPosition: googlepostion,
onMapCreated: (GoogleMapController controller){
_controllerGoogleMap.complete(controller);
newGoogleMapController=controller;
setState(() {
mapbottompadding=300.0;
});
locatepostion();
and here is my output
-------Update
it's working perfectly on andriod phone, but not working on emulator.
Upvotes: 22
Views: 26398
Reputation: 4739
Faced this problem while working on a udemy project clima_flutter
Was getting below errors while launching the app
Flutter: Unhandled Exception: User denied permissions to access the device's location
My flutter version is 3.16.9
flutter --version
Flutter 3.16.9 • channel stable • https://github.com/flutter/flutter.git
And my dart version is 3.2.6
dart --version
Dart SDK version: 3.2.6 (stable) (Wed Jan 24 13:41:58 2024 +0000) on "macos_x64"
The gradle wrapper version I am using is 7.5
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
Using below dependency for geolocator
geolocator: ^11.0.0
Was trying to load current location at the app startup.
class _LoadingScreenState extends State<LoadingScreen> {
@override
void initState() {
super.initState();
getLocation();
}
Where getLocation calls the getCurrentLocation() method
Future<void> getLocation() async {
await Location().getCurrentLocation();
}
Pre-Requisite:
For android below permissions request should be added in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
And for ios, in ios/Runner/info.plist
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
Still my code was failing. And below implementation is a fix. Where we first check if the permission is available, if not then we request before hand.
Future<void> getCurrentLocation() async {
try {
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
print("No permission, requesting one");
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.deniedForever) {
return Future.error('Location Not Available');
}
}
print("Found permission to get location");
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.low);
latitude = position.latitude;
longitude = position.longitude;
} catch (e) {
print(e);
}
}
With this implementation a request dialog was prompted on the emulator to provide the location permission.
Upvotes: 1
Reputation: 53
To solve this follow the below steps:
Android: Project->android->src->main->AndroidManifest.xml
add permission inside the tag
"<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION />"
IOS: Project->ios->Runner->Info.plist
add permission inside the tag
"<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>"
Call method on click of your button
void getLocation() async{
LocationPermission permission;
permission = await Geolocator.requestPermission();
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position); //here you will get your Latitude and Longitude
}
This solution worked for me, Hope it would helpful for you too.
Upvotes: 2
Reputation: 21
Please also check your permission request. For example: Your permission status is under this button; "Find my location"
ElevatedButton(onPressed: () async {
PermissionStatus location = await Permission.location.request();
locationInfo();
}, child: const Text("Find my location")),
Upvotes: 0
Reputation: 381
This is a common issues in geolocator that arises esp. when you don't grant permission to the device. By default it is disabled, so you have to request for it when the locatePosition()
is called. Remember that the geolocator package is wrapped under google_maps_flutter.
So to solve the location problem, ensure you do the following
class _MapScreenState extends State<MapScreen> {
void locatePosition() async {
bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
await Geolocator.checkPermission();
await Geolocator.requestPermission();
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
currentPosition = position;
LatLng latLngPosition = LatLng(position.latitude, position.longitude);
// Ask permission from device
Future<void> requestPermission() async {
await Permission.location.request();
}
}
Upvotes: 9
Reputation: 736
class GeolocatorService {
Future<Position?> determinePosition() async {
LocationPermission permission;
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.deniedForever) {
return Future.error('Location Not Available');
}
} else {
throw Exception('Error');
}
return await Geolocator.getCurrentPosition();
}
}
Upvotes: 18
Reputation: 668
LocationPermission permission;
permission = await Geolocator.requestPermission();
request for the permission first by using this.
Upvotes: 37