Permisson handler - location do not work (Flutter) - flutter

I have a problem with Permisson Handler while try to use location.
When i tap and run gps func everything seems fine. Pop up is showing up I can choose options - Allow, Allow while Using etc, however when I choose allow or allow while it do not turn on location. It was working perfectly and just stopped somehow (permission is set to granted).
When I turn it on manually (Location on my device) everything is like it was before. I don't now why this prompt stops turning on location service.
void _onGpsTap() async {
bloc.emitEvent(DisplayProgressIndicator());
try {
permissionStatus = await Permission.location.status;
if (permissionStatus.isUndetermined) {
permissionStatus = await Permission.location.request(); // Here I'm asking for turning location on
}
if (permissionStatus.isDenied) {...
I'm using
geolocator: ^5.3.1
permission_handler: 5.0.0+hotfix.3

Try out below code:-
var status = await Permission.location.request();
if(status.isGranted){
log("Permission Granted");
getAddress();
}
getAddress() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
latitude = position.latitude;
longitude = position.longitude;
if(latitude != null && longitude != null){
apiCall();
}
}
permission_handler: ^8.1.4+2
geolocator: ^7.2.0+1

I found out that is not possible to turn on location service via. flutter and the user have to do it maunally.

Related

Flutter geolocator not able to get the exact current position and it always return same latitude and longitude value

Here's my code
void getlocation() async {
LocationPermission per = await Geolocator.checkPermission();
if (per == LocationPermission.denied ||
per == LocationPermission.deniedForever) {
print("permission denied");
LocationPermission per1 = await Geolocator.requestPermission();
} else {
Position currentLoc = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best);
setState(() {
long = currentLoc.longitude.toString();
lat = currentLoc.latitude.toString();
});
}
}
Here's the output when i return long and lat value to my screen and the value will always be the same. even i manually set the position to other places, it will go back to this position again later on
output
The Geolocator package returns the default location on your emulator. If you are using an android emulator, you can manually change the location from the emulator settings here, after which you have to open google maps on the emulator and tap on the get location button so the emulator registers this new location.
Thereafter, you can go back and run your app. should have this new location you set showing up.

Unhandled Exception: PlatformException(PERMISSION_DENIED, Access to location data denied, null)

it give the exception when i get the current location of the user . my flutter version :-
v1.17.4, and my info.plist code is given below. geolocator: ^5.3.2+2
Future<Position> locateUser() async {
return await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.low,locationPermissionLevel:
GeolocationPermission.location);
}
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
It might be late to reply but I was also facing the same problem where the app was not asking for permission in iOS and was working perfectly fine in android.
Because it was not asked for permission that's why the permission code was not working for iOS. I found a package named "location_permissions" which can be used to ask for permission manually.
Steps to do are following
Add "location_permissions: 3.0.0+1" this dependencies in "pubspec.yaml". Please note that I did that for flutter 1.22.0 so for flutter 2.0 this might be an issue.
Import the package in the file
import 'package:location_permissions/location_permissions.dart';
Add the following code on the page where you want to ask for permission. (Better to add that on the very first page of your app.)
#override
void initState() {
....
if (Platform.isIOS) {
location_permission();
}
....
}
Add the following two methods in the same file
void location_permission() async {
final PermissionStatus permission = await _getLocationPermission();
if (permission == PermissionStatus.granted) {
final position = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best);
// Use the position to do whatever...
}
}
Future<PermissionStatus> _getLocationPermission() async {
final PermissionStatus permission = await LocationPermissions()
.checkPermissionStatus(level: LocationPermissionLevel.location);
if (permission != PermissionStatus.granted) {
final PermissionStatus permissionStatus = await LocationPermissions()
.requestPermissions(
permissionLevel: LocationPermissionLevel.location);
return permissionStatus;
} else {
return permission;
}
}
That's it now you should get a popup in the iOS app which will ask for the permission of location.
I was facing the same issue. I was running on IOS simulator
Issue Resolved on calling requestPermission() method
LocationPermission permission = await Geolocator.requestPermission();
Future<Position> position =
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
In info.plist:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to your location.</string>
dependency in pubspec.yaml:
geolocator: ^8.0.5

Flutter Geolocator Location doesn't obtain for real phones

I used geolocator flutter package for my little app and it works perfectly with emulator, but when I try to use in my real smart phone, it doesnt work. Do you have any idea?
void getLocationData() async {
WeatherModel weatherModel = WeatherModel();
var weatherData = await weatherModel.getLocationWeather();
//print(weatherData);
var zipCode = await weatherModel.getLocationZipCode();
zipCode = zipCode.toString().substring(0, 2);
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LocationScreen(
locationWeather: weatherData,
zipCode: zipCode,
);
}));
}
Future<dynamic> getLocationWeather() async {
Location location = Location();
await location.getCurrentLocation();
NetworkHelper networkHelper = NetworkHelper(
'$openWeatherMapURL?lat=${location.latitude}&lon=${location.longitude}&appid=$apiKey&units=metric');
var weatherData = await networkHelper.getData();
return weatherData;
}
Angela Yu's student? her projects need one update for optimizing and fixing little errors.
I got problems with Geolocator, it was not being used correctly, and this site helped me a lot. Then I made my own Location class with Provider and... life becomes good again.
Try use geolocator. I seems you are not using geolocator package
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
// return true if location service is enable, false if not
bool locationServiceEnable = await geolocator.isLocationServiceEnabled();
// position instace
Position position;
// if location service enable get current position
if (locationServiceEnable) {
// await current position
position = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best);
// if location service is not enable get last known position
} else {
// await last known position
position = await geolocator.getLastKnownPosition(
desiredAccuracy: LocationAccuracy.best);
}
if (position != null) {
// fetch weather data with position data
}
You should add
<uses-permission android:name="android.permission.INTERNET" />
to the AndroidManifest.xml. It helps to use the internet to fetch location data in an Android device, because your app still need to get data from URL openweathermap.
using geolocator: ^7.0.1
and adding
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
to AndroidManifest.xml file
void getLocation() async {
print('getLocation');
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position);
}
and using in onPressed
onPressed: () {
getLocation();
},
solves the problem.

Flutter Location package requestService() never returns

I have a function like this. Using Location package from flutter it shows the dialog to enable GPS.
Future<bool> _checkServiceStatus() async {
final Location location = Location();
bool serviceStatus = await location.serviceEnabled();
if (!serviceStatus) {
serviceStatus = await location.requestService();
print('status -> $serviceStatus');
}
return serviceStatus;
}
When its calling await location.requestService(), it is showing the dialog to enable GPS but after that it never returns the result.
Even its not executing the print() function.
What am i doing wrong here?
Any help would be very appreciated! Thanks in advance.
I had the same issue. It could be solved by upgrading your Flutter project, follow this link https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects
Try this code to check wather permission enabled, service enabled than it returns true else false. Must configure "location" package related configuration in android and ios projects.
Future<bool> checkServiceStatus() async {
final Location location = Location();
final locationPermission = await location.hasPermission();
if (locationPermission == PermissionStatus.granted) {
final locationServiceEnabled = await location.serviceEnabled();
if (locationServiceEnabled == true) {
return true;
} else {
final requestServiceStatus = await location.requestService();
if (requestServiceStatus == true) {
return true;
} else {
BotToast.showSimpleNotification(
title: "Enable GPS to allow this feature");
return false;
}
}
} else {
BotToast.showSimpleNotification(title: "Required location permission to allow this feature");
return false;
}
}

How to get call back of Google location service dialog button click's in flutter?

I need to fetch current location of device in my Flutter App. I have added plugins are location: ^2.3.5 and geolocator: ^5.0.1 to get current location.
All is working fine but when the GPS of my device is off(disabled) then it shows a dialog that i have shown in Image please check it.I need call back on press ok button so that i can get current location and execute next code lines. I will be thankful to you if you help me.
Here have some code.
getCurrentLocation() async{
try {
await location.getLocation().then((locationData){
if(locationData!=null){
moveHomeWithLatLon(context,false,locationData.latitude.toString(),locationData.longitude.toString());
}else{
dialogInternetCheck(context, alert, "Please check location services on device");
}
});
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'Permission denied';
dialogInternetCheck(context, alert, "Location services "+error);
}
}
}
You should check for the service status result after requesting location service. If it is true that means the user has provided the access. I have provided the example below
bool serviceStatusResult = await location.requestService();
print("Service status activated after request: $serviceStatusResult");
if (serviceStatusResult) {
await _getCurrentLocation();
}
Future<bool> _getCurrentLocation() async {
try {
Location location = Location();
LocationData userLocation = await location.getLocation();
} on PlatformException catch (e) {
Log.info("Location fetch failed : ${e.toString()}");
}
return Future.value(true);
}