Hexadecimal Mobile Logo
Open Menu

Introduction

How to Use Geolocator Plugin in Flutter

Image Source: google

Getting the user’s location is essential for many modern apps — whether you’re building a delivery service, travel guide, or fitness tracker. The Geolocator plugin in Flutter makes it simple to retrieve the device’s latitude and longitude with just a few lines of code.

In this blog, we’ll walk you through how to install the plugin, set up permissions, and display the location in your Flutter app.

Installing the Plugin

First, open your pubspec.yaml file and add the geolocator dependency :

yaml
dependencies:
  flutter:
    sdk: flutter
  geolocator: ^9.0.2
  • Run the following command in your terminal to install it

flutter pub get


Need location-based features in your Flutter app? Let Hexadecimal build it.

Need location-based features in your Flutter app? Let Hexadecimal build it.

Talk to Our ExpertsArrow

Requesting Permissions

Before you can get the location, you need to request the necessary permissions from the user. Update your Android and iOS configuration files :


For Android : Add this to android/app/src/main/AndroidManifest.xml:


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

For iOS : Add this to ios/Runner/Info.plist:

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to your location to provide better service.</string>

Fetching the Current Location

Now that permissions are in place, let’s fetch the location using Geolocator :


import 'package:geolocator/geolocator.dart';

Future<Position> _determinePosition() async {
  bool serviceEnabled;
  LocationPermission permission;

  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    return Future.error('Location services are disabled.');
  }

  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      return Future.error('Location permissions are denied.');
    }
  }
  
  if (permission == LocationPermission.deniedForever) {
    return Future.error('Location permissions are permanently denied.');
  } 

  return await Geolocator.getCurrentPosition();
}

You Might Also Like

Displaying Location in the UI

Here’s a simple example of displaying the location in your Flutter widget :


import 'package:flutter/material.dart';

class LocationScreen extends StatefulWidget {
  @override
  _LocationScreenState createState() => _LocationScreenState();
}

class _LocationScreenState extends State<LocationScreen> {
  String location = "Press the button to get location";

  void _getLocation() async {
    try {
      Position position = await _determinePosition();
      setState(() {
        location = "${position.latitude}, ${position.longitude}";
      });
    } catch (e) {
      setState(() {
        location = e.toString();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Geolocator Example")),
      body: Center(child: Text(location)),
      floatingActionButton: FloatingActionButton(
        onPressed: _getLocation,
        child: Icon(Icons.location_on),
      ),
    );
  }
}

Want real-time location tracking in your app? We can make it happen.

Want real-time location tracking in your app? We can make it happen.

Explore Our ServicesArrow

Best Practices for Location Usage

  • Always explain why your app needs location access.

  • Request permissions only when necessary.

  • Respect user privacy and store location data securely.

  • Use getCurrentPosition() for one-time location and getPositionStream() for continuous tracking.

FAQs

Q.1. What is the Geolocator plugin in Flutter?
A: The Geolocator plugin provides easy-to-use APIs to get the device's location in Flutter apps.

Q.2. Do I need internet to use Geolocator?
A: GPS works offline, but certain features like reverse geocoding may require internet access.

Q.3. How do I handle permission errors?
A: Use Geolocator.checkPermission() and Geolocator.requestPermission() to handle permissions gracefully.

Q.4. Can I track location continuously?
A: Yes, use getPositionStream() for real-time location updates.

Q.5. Is the Geolocator plugin free?
A: Yes, it's open-source and available on pub.dev.

Scroll to top arrow
Grid background

Buy, Sell & Rent Properties – Download HexaHome App Now!

  • Search Icon

    Find your perfect home

  • House Icon

    Post your property at ₹0

Available on iOS & Android

download-playstoredownload-ios
mobile-app-banner

A Product By Hexadecimal Software Pvt. Ltd.