Device GPS Location in Flutter

Flutter Queen✨
3 min readMay 14, 2023

--

One of the important features of a mobile application is to access the device’s GPS location. In this article, we will walk through the steps to enable the GPS location on a device in a Flutter application.

Step 1: Add the Location package

To access the device’s GPS location in Flutter, we need to add the location package to our application. The location package provides a set of classes and methods to interact with the device’s location services.

dependencies:
flutter:
sdk: flutter
location: ^4.3.0

For Android, we need to add the following permissions to the `AndroidManifest.xml` file:

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

For iOS, we need to add the following permission to the Info.plist file:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Reason why we need access to your location</string>

Step 2: Request Permission

Before accessing the device’s GPS location, we need to request permission from the user. The user can either grant or deny the permission. In case of denial, we need to handle the error gracefully. To request permission, we can use the requestPermission() method of the location package.

import 'package:location/location.dart';

class LocationService {
Location location = Location();

Future<bool> requestPermission() async {
final permission = await location.requestPermission();
return permission == PermissionStatus.granted;
}
}

Step 3: Get the current location

After the permission is granted, we can get the device’s current location using the getLocation() method of the Location class. The method returns a LocationData object that contains the latitude, longitude, altitude, and accuracy of the location.

import 'package:location/location.dart';

class LocationService {
Location location = Location();

Future<bool> requestPermission() async {
final permission = await location.requestPermission();
return permission == PermissionStatus.granted;
}

Future<LocationData> getCurrentLocation() async {
final locationData = await location.getLocation();
return locationData;
}
}

Step 4: Check if the GPS is enabled

Before requesting the location, we need to check whether the GPS is enabled on the device. If the GPS is not enabled, we need to ask the user to enable it. To check if the GPS is enabled, we can use the serviceEnabled() method of the Location class. If the method returns false, we can use the requestService() method of the Location class to ask the user to enable the GPS. Here is an example code to check if the GPS is enabled:

import 'package:location/location.dart';

class LocationService {
Location location = Location();

Future<bool> requestPermission() async {
final permission = await location.requestPermission();
return permission == PermissionStatus.granted;
}

Future<LocationData> getCurrentLocation() async {
final serviceEnabled = await location.serviceEnabled();
if (!serviceEnabled) {
final result = await location.requestService;
if (result == true) {
print('Service has been enabled');
} else {
throw Exception('GPS service not enabled');
}
}

final locationData = await location.getLocation();
return locationData;

Conclusion

In this article, we learned how to enable the device’s GPS location in a Flutter application. We added the location package to our application, requested permission from the user, checked if the GPS is enabled, and retrieved the device’s current location. We also added the required permissions to the Android Manifest and Info.plist files. With these steps, we can now build powerful location-aware Flutter applications.

I hope this article was helpful to you. Thank you for taking the time to read it. Your feedback and suggestions are always welcome.

Support Me:

--

--

Flutter Queen✨
Flutter Queen✨

Written by Flutter Queen✨

Flutter Enthusiast | Software Engineer to be

No responses yet