Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Example usage of geolocator in Flutter

Example usage of geolocator in Flutter

Posted on August 19, 2025 by Toma Velev

Here’s an example of how to use the geolocator package in Flutter:

First, add the following dependency to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter

  geolocator: ^14.0.2

Then, run flutter pub get to install the package.

Now, here’s an example of how to use it in a Flutter app:

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() {
  runApp(MyApp());
}

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _currentPosition = '';
  bool _isLocationServiceEnabled = false;
  bool _isGeolocationServiceEnabled = false;

  @override
  void initState() {
    super.initState();
    _checkLocationService();
  }

  Future<void> _checkLocationService() async {
    bool serviceEnabled;
    LocationPermission permission;

    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      // Open the settings menu
      _openAppSettings();
    }

    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        setState(() {
          _isLocationServiceEnabled = false;
        });
      } else {
        setState(() {
          _isLocationServiceEnabled = true;
        });
      }
    } else if (permission == LocationPermission.deniedForever) {
      setState(() {
        _isLocationServiceEnabled = false;
      });
    } else {
      setState(() {
        _isLocationServiceEnabled = true;
      });
    }
  }

  Future<void> _openAppSettings() async {
    await launchUrl(Uri.parse('https://support.google.com/locationhistory/answer/1719578'));
  }

  Future<void> _getCurrentPosition() async {
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: DesiredAccuracy.high);
    setState(() {
      _currentPosition = 'Latitude: ${position.latitude}, Longitude: ${position.longitude}';
    });
  }

  Future<void> _getLastKnownPosition() async {
    Position position = await Geolocator.getLastKnownPosition(desiredAccuracy: DesiredAccuracy.high);
    setState(() {
      _currentPosition = 'Latitude: ${position.latitude}, Longitude: ${position.longitude}';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Geolocator Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_currentPosition),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _isLocationServiceEnabled? _getCurrentPosition : null,
              child: Text('Get Current Position'),
            ),
            SizedBox(height: 10),
            ElevatedButton(
              onPressed: _isLocationServiceEnabled? _getLastKnownPosition : null,
              child: Text('Get Last Known Position'),
            ),
          ],
        ),
      ),
    );
  }
}

This example shows how to:

  1. Check if the location service is enabled.
  2. Request permission for the app to access the user’s location.
  3. Get the current position of the device.
  4. Get the last known position of the device.

Note that you need to add the geolocator package and import it in your Dart file for this example to work.

Also, remember that the geolocator package requires the user’s permission to access their location. If you’re testing this on a physical device, make sure to grant the app permission in your device’s settings. If you’re testing this on an emulator, make sure to add the necessary permissions in your AndroidManifest.xml file.

  • Join iOS Beta Testing Explained
  • Firebase App Distribution Setup
  • iOS App Lifetime Unverified
  • Flutter Bottom Border
  • Get Flutter current time zone

Categories

  • Apps (25)
  • ChatGPT (24)
  • Choosing a Framework (38)
  • Flutter (279)
  • Graphical User Interface (14)
  • Marketing (118)
  • Software Development (288)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (4)
  • Uncategorized (14)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (86)
    • Flutter Apps (26)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Join iOS Beta Testing Explained
  • Firebase App Distribution Setup
  • iOS App Lifetime Unverified
  • Flutter Bottom Border
  • Get Flutter current time zone

Post Categories

  • Apps (25)
  • ChatGPT (24)
  • Choosing a Framework (38)
  • Flutter (279)
  • Graphical User Interface (14)
  • Marketing (118)
  • Software Development (288)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (4)
  • Uncategorized (14)
  • Vaadin (15)