Open In App

Geolocator in Flutter

It is extremely common in apps like payment, maps, and e-commerce to access the location of users. Location accessing is also vastly used in item tracking. In this article, we will be looking at the following – 

This article focuses on user location from Android only. 



Add the dependency

To use geolocator, we will first install it as a dependency in pubspec.yaml file. To do so mention geolocator under the dependencies section.



Import the dependency




import 'package:geolocator/geolocator.dart';

Set-Up

Before we move to the coding part we have to do some settings.

1. In android/app/build.gradle set compileSDKVersion to 31

2. Give the user permission to access the device location in the android/app/src/main/AndroidManifest.xml file.

3. In android/gradle.properties set the following properties : 

Implementation

Let us define the Position in the class that we will talk about throughout the article. The position is a non-nullable variable.




Position? position;

Request Permission

If the app requires permission from the user to access the location we use requestPermission() method on Geolocator so that it can perform location operations.




LocationPermission permission = await Geolocator.requestPermission();

Check Permission

To find out if the location services are enabled or not, we can invoke checkPermission() on Geolocator.




LocationPermission permission = await Geolocator.checkPermission();

Get Current Location

Let us create a function getCurrentPosition() which call getCurrentPosition() method on Geolocator, and return the position of device.




getCurrentPosition() async {
    position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    return position;
  }

Output:

When we press the GET button, it invokes getCurrentPosition() and returns position which gives latitude and longitude that we display on the screen.

Get Last Location

To get the last location we use getLastKnownPosition() method. We are defining getLastPosition() method to get the last known position inside it and then return it. The position returned will also tell the longitude and latitude of the device.




getLastPosition() async {
    position = await Geolocator.getLastKnownPosition();
    return position;
  }

Output:

Access Continuous Updates of Location

To get the continuous location updates, we need to subscribe to the service Stream as – 




StreamSubscription<ServiceStatus> serviceStatusStream =
        Geolocator.getServiceStatusStream().listen((ServiceStatus status) {
      print(status);
    });

We can set the location settings like the accuracy we want and distantFilter, it will give the location whenever the device moves distantFilter distance horizontally.  




final LocationSettings locationSettings = LocationSettings(accuracy: LocationAccuracy.best, distanceFilter: 0);

Then, we defined a function getLocationUpdates() to invoke when we want to get the location updates.




getLocationUpdates() {
    final LocationSettings locationSettings =
        LocationSettings(accuracy: LocationAccuracy.best, distanceFilter: 0);
  
    StreamSubscription<ServiceStatus> serviceStatusStream =
        Geolocator.getServiceStatusStream().listen((ServiceStatus status) {
      print(status);
    });
  }

Output:

The location that we are getting is constant because the device is not moving. 


Article Tags :