Sitemap
Slime

Slime

If you enjoy my articles, consider buying me a coffee to support my work! buymeacoffee.com/slime8992

Flutter — Geolocator for Accurate Location Tracking

5 min readNov 6, 2025

--

Modern applications rely heavily on rich contextual information — and location is one of the most powerful forms of context an app can have. From food delivery to ride-hailing, from fitness tracking to weather forecasting, knowing the user’s current location enables your app to become smarter, more personalized, and truly useful.

That’s where Flutter’s geolocator package comes in.

If your app needs to obtain the user’s location, track movement in real time, calculate distances, or even detect geofences, geolocator is undoubtedly the go-to tool trusted by thousands of Flutter developers.

Press enter or click to view image in full size

🌍 What is Geolocator?

Geolocator is a powerful Flutter plugin that allows your app to access location services on multiple platforms including Android, iOS, Web, and Desktop.

It provides a clean and unified API for:

  • Getting the current latitude and longitude coordinates
  • Listening to continuous location updates
  • Calculating the distance between two points
  • Checking and requesting location permissions
  • Determining whether the device’s location services are enabled

In short — it gives your app the ability to sense the user’s surrounding world.

🚀 Why Choose Geolocator?

Technically, you could build location tracking from scratch using native APIs — but that path is complex and full of challenges.

Geolocator abstracts away all that complexity and provides a clean, Dart-friendly interface.

Key Advantages:

  • Cross-platform support: Works seamlessly on Android, iOS, Web, and Desktop.
  • Permission management: Built-in mechanisms for Android and iOS location permissions.
  • High accuracy tracking: Supports multiple precision levels (low, medium, high).
  • Real-time updates: Continuously streams user movement data.
  • Distance calculation: Easily compute the distance between coordinates.
  • Simple configuration: Clean API with no need for platform-specific code.

Whether you’re building a delivery app, a fitness tracker, or a weather app, this plugin is a must-have in your Flutter toolkit.

⚙️ Installation Guide

Setting up geolocator is quick and simple.

Step 1: Add the dependency

In your pubspec.yaml file, add the following line:

dependencies:
geolocator: ^13.0.1

Then run:

flutter pub get

Step 2: Configure platform permissions

🟢 Android:Add the following permissions in the android/app/src/main/AndroidManifest.xml file:

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

And make sure your <application> tag contains:

<meta-data android:name="flutterEmbedding" android:value="2" />

🍏 iOS:Open the ios/Runner/Info.plist file and add:

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to your location.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs location access to track your position.</string>

After configuration, you’re ready to start locating users within your Flutter app.

🧪 Using Geolocator in Your Flutter App

Here are the steps to get started.

Get Slime’s stories in your inbox

Join Medium for free to get updates from this writer.

✅ Get your current location
This code shows how to get the user’s current latitude and longitude:

import 'package:geolocator/geolocator.dart';

Future<void> _getCurrentLocation() async {
bool serviceEnabled;
LocationPermission permission;
// Check if location services are enabled
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if ( ! serviceEnabled) {
// If the service is not enabled, throws an error
return Future.error('Location services are disabled.');
}
// Check the status of permissions
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
// Request permission if permission is denied
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// If the request is still rejected, throw an error
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// If permissions are permanently denied, throw an error
return Future.error('Location permissions are permanently denied, cannot request.');
}
// Get current location
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print('Latitude: ${position.latitude}, Longitude: ${position.longitude}');
}

This can help you get an instant snapshot of the user’s location.

🛰️ Real-time location update stream
Need real-time tracking (e.g. Didi or food delivery apps)? You can use the getPositionStream() method:

StreamSubscription<Position> positionStream =
Geolocator.getPositionStream(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 10, // Updates only when moving 10 meters
),
).listen((Position position) {
print('${position.latitude}, ${position.longitude}');
});

This stream continuously emits location updates as users move.

📏 Calculate the distance between two points
geolocator can even calculate the distance between two geographic coordinate points in meters:

double distanceInMeters = Geolocator.distanceBetween(
12.9716, 77.5946, // start point
13.0827, 80.2707, // end point
);

print('Distance: $distanceInMeters meters');

Ideal for applications that display travel distances, delivery ranges, or motion trajectory summaries.

Advanced Tips for Developers

Here are some professional-level techniques to help your Flutter app truly stand out:

1. Optimize Battery Usage

High-accuracy location tracking can significantly drain battery power.

When you only need an approximate location, use LocationAccuracy.low instead.

desiredAccuracy: LocationAccuracy.low

2. Handle Permission Denials Gracefully

Always manage permission-denied scenarios in a user-friendly way — for example, by showing an explanatory dialog and guiding users to system settings when necessary.

3. Use Distance Filters Wisely

By setting a distanceFilter (e.g., only updating location when the user moves more than 10 meters), you can avoid unnecessary location updates and improve performance.

4. Handle Network Interruptions

When network connectivity is lost, use a caching mechanism to store the user’s last known location, ensuring your app can still function smoothly offline.

⚠️ Avoid Common Mistakes

  • ❌ Forgetting to add required iOS permissions in the Info.plist file.
  • ❌ Not checking whether location services are enabled on the device.
  • ⚠️ Requesting unnecessarily high-accuracy location updates (which quickly drains the battery).
  • 💡 Ignoring scenarios where users deny permission requests.

Real-World Use Cases

Geolocator provides robust support for all kinds of location-based Flutter apps:

  • Ride-hailing and delivery apps — to track drivers and passengers in real time.
  • Fitness and health apps — to record running, walking, or cycling routes.
  • Travel apps — to recommend nearby attractions or restaurants.
  • Weather apps — to fetch weather data based on the user’s current location.
  • E-commerce apps — to automatically detect the user’s address for delivery purposes.

If your app needs to know “where the user is,” Geolocator will be your most powerful ally.

In an era that emphasizes personalization, location awareness can elevate your app from ordinary to intelligent.

The geolocator package allows you to effortlessly add precise, real-time location tracking to your Flutter apps — all without writing a single line of native code.

It’s stable, well-documented, and feature-rich, making it suitable for everything from personal projects to full-scale production applications.

--

--

Slime
Slime

Written by Slime

If you enjoy my articles, consider buying me a coffee to support my work! buymeacoffee.com/slime8992