Location and GPS help apps know where you are in the real world. This lets apps give you directions, find nearby places, or track your movement.
0
0
Location and GPS in Flutter
Introduction
When you want to show your current position on a map.
When you need to find nearby restaurants or stores.
When you want to track your running or walking route.
When your app needs to give directions from one place to another.
When you want to tag photos or posts with your location.
Syntax
Flutter
import 'package:geolocator/geolocator.dart'; Future<Position> getCurrentLocation() async { bool serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { return Future.error('Location services are disabled.'); } LocationPermission 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 need to add the geolocator package to your Flutter project.
Always check and request permission before accessing location.
Examples
This gets the current GPS position once.
Flutter
Position position = await Geolocator.getCurrentPosition();
This listens for location updates continuously.
Flutter
Stream<Position> positionStream = Geolocator.getPositionStream();
positionStream.listen((Position position) {
print(position.latitude);
print(position.longitude);
});This checks if the device's location service is turned on.
Flutter
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
Sample App
This app shows a button. When you press it, it asks for your current GPS location and shows the latitude and longitude on screen.
Flutter
import 'package:flutter/material.dart'; import 'package:geolocator/geolocator.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { String locationMessage = 'Press the button to get location'; void _getLocation() async { try { Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); setState(() { locationMessage = 'Latitude: ${position.latitude}, Longitude: ${position.longitude}'; }); } catch (e) { setState(() { locationMessage = 'Error: $e'; }); } } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Location and GPS Example')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(locationMessage, textAlign: TextAlign.center), SizedBox(height: 20), ElevatedButton( onPressed: _getLocation, child: Text('Get Current Location'), ), ], ), ), ), ); } }
OutputSuccess
Important Notes
Remember to add location permissions in your AndroidManifest.xml and Info.plist files.
Test location features on a real device or emulator with location simulation.
Location accuracy affects battery life; choose accuracy wisely.
Summary
Location and GPS let apps know where you are in the world.
Always check and ask for permission before using location.
You can get location once or listen for updates continuously.