14

Hi as I wrote in the title how can I achieve this? I've got the location permission but can not turn the location on!

Moein Hosseini
  • 1,514
  • 3
  • 15
  • 33

6 Answers6

8

It will open setting. so user can enable it. it is best practice as per my knowledge.

Install plugin :

https://pub.dartlang.org/packages/android_intent#-installing-tab-

Import in your dart :

import 'package:android_intent/android_intent.dart';

Add method :

void openLocationSetting() async {
    final AndroidIntent intent = new AndroidIntent(
      action: 'android.settings.LOCATION_SOURCE_SETTINGS',
    );
    await intent.launch();
  }

Invoke it done...

Khurshid Ansari
  • 4,638
  • 2
  • 33
  • 52
6

I'll be using the things I learnt from this link to guide you.

Add this dependency to pubspec.yaml

dependencies:
  location: ^3.0.0

Now, import the following package to your .dart file

import 'package:location/location.dart';

Now inside a function, to ask for the user's location using a pop-up box, you can do-

Location location = new Location();
bool _serviceEnabled;
LocationData _locationData;

The above were the declarations that we will be using in our following code -

_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
  _serviceEnabled = await location.requestService();
  if (!_serviceEnabled) {
    debugPrint('Location Denied once');
  }
}

Now, you can use the above snippet as per your requirement to make Location request calls using a pop-up for whatever number of times you want.

If the user's locations has been granted, you can use the following line to store and use the location data.

_locationData = await location.getLocation();

If the location is not granted, then the above line would result in the program failing, so be sure to only use the above line when the user has allowed location access from the pop-up.

Hope this helps. Cheers!

4

flutterlocation Plugin shows you the dialogue to turn on gps as like native android

Rajesh
  • 3,562
  • 7
  • 24
  • 43
3

First add this dependency in pubspec.yaml:

location: ^1.4.0

Then, use this function to retrieve current location of your device:

import 'package:location/location.dart';
 fetchCurrentLocation() async {

  print("STARTING LOCATION SERVICE");
  var location = Location();
  location.changeSettings(accuracy: LocationAccuracy.POWERSAVE,interval: 1000,distanceFilter: 500);
  if (!await location.hasPermission()) {
    await location.requestPermission();
  }

  try {
    await location.onLocationChanged().listen((LocationData currentLocation) {
      print(currentLocation.latitude);
      print(currentLocation.longitude);
      latitude = currentLocation.latitude;
      longitude = currentLocation.longitude;
    });
  } on PlatformException {
    location = null;
  }

}

Aarif Ali
  • 11
  • 2
  • 14
Rachit Rawat
  • 2,410
  • 3
  • 19
  • 30
  • 5
    Actually, I'm using this dependency to get the location but it does not get the permissions so I do it manually by simple-permissions. if I turn off the location this code goes into catch block without asking anything to turn on the location or getting permissions! – Moein Hosseini Aug 29 '18 at 04:39
  • 1
    @Rachit Rawat : your an give lat and long but question how to enable gps or location service in android ? – Khurshid Ansari Dec 18 '18 at 11:18
  • The question is to Ask for ON/OFF GPS.....not for how to get location..... – Himanshu Jan 29 '20 at 11:32
2

You can use System Dialog and ask user to enable it. Check this link for your reference.

Create a platform specific channel and call it when user want to enable it.

Yuvarajan
  • 77
  • 1
  • 11
1

I did the same but sometimes pop-up notification of location permission doesn't appear.

I add these lines in the following location:
project/android/app/src/main/AndroidManifest.xml

F. Müller
  • 3,969
  • 8
  • 38
  • 49