Working with Geolocation in Flutter
GEO LOCATION EXAMPLE
GPS(Global Positioning System) has become a standard feature on modern smartphones. It's usually used by applications to get the device location. This blog shows you how to access device location in Flutter using GPS, including how to get permissions, get current location and continuous location update.
Code
GPS(Global Positioning System) has become a standard feature on modern smartphones. It's usually used by applications to get the device location. This blog shows you how to access device location in Flutter using GPS, including how to get permissions, get current location and continuous location update.
Dependencies
dependencies{
geolocator: ^3.0.1
}
A Flutter package
geolocator
provides geolocation functionalities. Add it in your pubspec.yaml
file and run Get Packages.Permissions
You need to add permissions to each platform. For Android, you need
ACCESS_COARSE_LOCATION. Add the following in AndroidManifest.xml
.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
For iOS, you need NSLocationWhenInUseUsageDescription
permission. Add it in the Info.plist
file.
<key>NSLocationWhenInUseUsageDescription</key> |
We need to create an instance of
Geolocator
and store the value of latest Position
. The application will use the Position
value to display the latitude and the longitude.import 'dart:async'; import 'package:flutter/material.dart'; | ||||
import 'package:geolocator/geolocator.dart';
| ||||
class GeolocationExampleState extends State<GeolocationExample> {
| ||||
Geolocator _geolocator;
| ||||
Position _position;
@override void initState(){ super.initState(); _geolocator = Geolocator(); } @override Widget build(BuildContext context){ return Scaffold( appBar: AppBar( title:Text('Flutter Geolocation'),), body: Center( child:Text(
| ||||
Check Permission
|
If you've added the right permissions, the application will be granted with permissions to access the device location using GPS. In Android 6.0 and above, it will ask the user to grant the permission. But if you need to check whether the application has permission to access location, you can do it programatically. To do so, use
checkGeolocationPermissionStatus
method which returns Future<GeolocationState>. Optionally, for iOS, you can check locationAlways
and locationWhenInUse
separately by passing locationPermission
optional parameter whose type is GeolocationPermission
void checkPermission() {
| |
_geolocator.checkGeolocationPermissionStatus().then((status) { print('status:
$status'); }); | |
_geolocator.checkGeolocationPermissionStatus(locationPermission:
GeolocationPermission.locationAlways).then((status) { print('always status: $status'); }); | |
_geolocator.checkGeolocationPermissionStatus(locationPermission:
GeolocationPermission.locationWhenInUse)..then((status) { print('whenInUse status: $status'); }); | |
}
Get Current Location Getting the current location is very simple. Just use getCurrentPosition method which returns Future<Location> . You can pass desiredAccuracy option. |
await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.hig
Get Location Updates
Updates are similar to the ‘location’ package but with enhanced options.
geolocator.getPositionStream().listen((position){// Do something here}); We can set the accuracy, time interval between updates, whether we want to force usage Android location manager, etc.
geolocator
.getPositionStream(LocationOptions( accuracy: LocationAccuracy.best, timeInterval: 1000)) .listen((position) { // Do something here});
Drop your questions and suggestions in the comments section if you have any...!
|
Comments
Post a Comment