geolocator 1.3.0 geolocator: ^1.3.0 copied to clipboard
Geolocation plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API for generic location (GPS etc.) functions.
Flutter Geolocator Plugin #
A Flutter geolocation plugin which provides easy access to the platform specific location services (LocationManager on Android and CLLocationManager on iOS).
Branch | Build Status |
---|---|
develop | |
master |
Features #
- Get the current location of the device;
- Get continuous location updates;
- Translate an address to geocoordinates and vice verse (a.k.a. Geocoding);
- Calculate the distance (in meters) between two geocoordinates.
Usage #
To use this plugin, add geolocator
as a dependency in your pubspec.yaml file. For example:
dependencies:
geolocator: '^1.3.0'
NOTE: There's a known issue with integrating plugins that use Swift into a Flutter project created with the Objective-C template. See issue Flutter#16049 for help on integration.
API #
Geolocation #
To query the current location of the device simply make a call to the getPosition
method:
import 'package:geolocator/geolocator.dart';
import 'package:geolocator/models/location_accuracy.dart';
import 'package:geolocator/models/position.dart';
Position position = await Geolocator().getPosition(LocationAccuracy.High);
To listen for location changes you can subscribe to the onPositionChanged
stream. Supply an instance of the LocationOptions
class to configure
the desired accuracy and the minimum distance change (in meters) before updates are send to the application.
import 'package:geolocator/geolocator.dart';
import 'package:geolocator/models/location_accuracy.dart';
import 'package:geolocator/models/position.dart';
var geolocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.High, distanceFilter: 10);
StreamSubscription<Position> positionStream = geolocator.getPositionStream(locationOptions).listen(
(Position position) {
print(_position == null ? 'Unknown' : _position.latitude.toString() + ', ' + _position.longitude.toString());
});
Geocoding #
To translate an address into latitude and longitude coordinates you can use the toPlacemark
method:
import 'package:geolocator/geolocator.dart';
import 'package:geolocator/models/placemark.dart';
Placemark placemark = await Geolocator().toPlacemark("Gronausestraat 710, Enschede");
If you want to translate latitude and longitude coordinates into an address you can use the fromPlacemark
method:
import 'package:geolocator/geolocator.dart';
import 'package:geolocator/models/placemark.dart';
Placemark placemark = await new Geolocator().toPlacemark(52.2165157, 6.9437819);
Calculate distance #
To calculate the distance (in meters) between two geocoordinates you can use the distanceBetween
method. The distanceBetween
method takes four parameters:
Parameter | Type | Description |
---|---|---|
startLatitude | double | Latitude of the start position |
startLongitude | double | Longitude of the start position |
endLatitude | double | Latitude of the destination position |
endLongitude | double | Longitude of the destination position |
import 'package:geolocator/geolocator.dart';
double distanceInMeters = await new Geolocator().distanceBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);
See also the example project for a complete implementation.
Permissions #
Android #
On Android you'll need to add the ACCESS_COARSE_LOCATION
and ACCESS_FINE_LOCATION
permissions to your Android Manifest. Todo so open the AndroidManifest.xml file and add the following two lines as direct children of the <manifest>
tag:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
iOS #
On iOS you'll need to add the NSLocationWhenInUseUsageDescription
to your Info.plist file in order to access the device's location. Simply open your Info.plist file and add the following:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
If you would like to access the device's location when your App is running in the background, you should also add the NSLocationAlwaysAndWhenInUseUsageDescription
(if your App support iOS 10 or earlier you should also add the key NSLocationAlwaysUsageDescription
) key to your Info.plist file:
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
Location accuracy #
The table below outlines the accuracy options per platform:
Android | iOS | |
---|---|---|
Lowest | 500m | 3000m |
Low | 500m | 1000m |
Medium | 100 - 500m | 100m |
High | 0 - 100m | 10m |
Best | 0 - 100m | ~0m |
Issues #
Please file any issues, bugs or feature request as an issue on our GitHub page.
Want to contribute #
If you would like to contribute to the plugin (e.g. by improving the documentation, solving a bug or adding a cool new feature), please carefully review our contribution guide and send us your pull request.
Author #
This Geolocator plugin for Flutter is developed by Baseflow. You can contact us at hello@baseflow.com