Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on device GPS in flutter?

Tags:

flutter

dart

By using simple_permissions and location package of Dart, it is only asking for user permission to ALLOW or DENY the app to use device location to show the current location of user on Google Maps. When i turn on the GPS manually on my device, I'm able to get the current location, but how can I ask the user to enable GPS from the app itself with a dialog to enable the GPS like Google Maps?

like image 793
Deepa Panicker Avatar asked Jan 28 '19 05:01

Deepa Panicker


2 Answers

you can user location library

var location = Location();

  Future checkGps() async {
    if (!await location.serviceEnabled()) {
      location.requestService();
    }
  }
like image 57
omar Avatar answered Oct 12 '22 04:10

omar


If you already have geolocator or if you are using the two (geolocator and location packages), then first import one of them as an alias, for example for location, import it as:

import 'package:location/location.dart' as loc;

loc.Location location = loc.Location();//explicit reference to the Location class
Future _checkGps() async {
if (!await location.serviceEnabled()) {
  location.requestService();
 }
}

Notice that use of var location = Location() or var location = loc.Location() wont work, you have to reference the Location class exclusively with the alias loc for the example above.

Then call the method in the initState as below to be checked every once the app starts as below:

@Override
void initState()
{
 super.initState();//comes first for initState();
 _checkGPS();
}
like image 20
ombiro Avatar answered Oct 12 '22 03:10

ombiro