How to Turn ON Device GPS in Flutter App

In this example, we are going to show you how to turn on Device GPS in Flutter App, or open the location setting for App in Flutter App. We will use the location and app_settings package to turn on Device GPS in Flutter. See the example below:

To show the location popup within the app, you can use location package in your Project. Add this package by adding the following lines in your pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  location: ^4.3.0

Now, Add location permission in AndroidManifest.xml file

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  

Import location package in your script:

import 'package:location/location.dart';

Now, Turn on your device GPS using the following code:

Location location = new Location();
bool ison = await location.serviceEnabled(); 
if (!ison) { //if defvice is off
  bool isturnedon = await location.requestService();
  if (isturnedon) {
      print("GPS device is turned ON");
  }else{
      print("GPS Device is still OFF");
  }
}

This code will show the following alert dialog within App.

If the user presses the "OK" button, the GPS will get turned ON.

In this method, we will show you how to open location settings for the app, so that the user can turn ON or OFF device GPS. To show the location setting, you need to add app_settings package in your project by adding the following lines in pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  app_settings: 4.1.1

Import the package in your script:

import 'package:app_settings/app_settings.dart';

Now, show the location setting from Flutter app:

AppSettings.openLocationSettings();

This code will redirect you to the location setting of the app:

import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:location/location.dart';

Future<void> main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    
    return  Scaffold(
          appBar: AppBar( 
              title: Text("Turn On GPS in Flutter"),
              backgroundColor: Colors.redAccent,
          ),
          body: Container( 
            alignment: Alignment.center,
            child: Column(
              children:[
                  
                  ElevatedButton(
                    onPressed: () async {
                        Location location = new Location();
                        bool ison = await location.serviceEnabled(); 
                        if (!ison) { //if defvice is off
                          bool isturnedon = await location.requestService();
                          if (isturnedon) {
                             print("GPS device is turned ON");
                          }else{
                             print("GPS Device is still OFF");
                          }
                        }
                    }, 
                    child: Text("Turn On GPS | Location Package")
                  ),

                  ElevatedButton(
                    onPressed: () async {
                       AppSettings.openLocationSettings();
                    }, 
                    child: Text("Turn On GPS | App Setting Package")
                  )
              ]
            )
          ),
           
       );
  }
}

Here, we have two buttons, one button will use the location package and another one will use the app_settings package to turn on device GPS.

In this way, you can turn on device GPS from Flutter app.

No any Comments on this Article


Please Wait...