geolocation 0.1.0 geolocation: ^0.1.0 copied to clipboard
Geolocation plugin for iOS and Android
Geolocation #
Flutter geolocation plugin that works on Android API 16+ and iOS 9+.
Features:
- Automatic permission management
- Retrieve last known location
- Request single location update
Plugin is under active development and will feature soon:
- Continuous location updates with foreground and background strategies
- Geocode
- Geofences
- Place suggestions
- Activity recognition
Documentation will improve as well.
Demo app #
Android | iOS |
---|---|
Getting Started #
Add geolocation to your pubspec.yaml:
dependencies:
geolocation: ^0.1.0
Note:
The iOS code of geolocation plugin is written in swift. There is a known issue for integrating swift written plugin into Flutter project created with Objective-C template. See #16049 for help on integration.
Permission #
In order to work with location, app needs to specify location permission in configuration file and request it at runtime. Geolocation plugin will check at runtime if permission configuration is missing, and throw an exception otherwise.
iOS configuration
Depending on the iOS version you target, you need to add to Infos.plist
:
- iOS 9/10:
NSLocationWhenInUseUsageDescription
orNSLocationAlwaysUsageDescription
- iOS 11+:
NSLocationAlwaysAndWhenInUseUsageDescription
orNSLocationWhenInUseUsageDescription
Depending if you specify permission description for when in use or always in Infos.plist
, geolocation plugin
will automatically request the proper associated permission at runtime.
Android configuration
You need to add one of the following permission to AndroidManifest.xml
:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Note that ACCESS_FINE_LOCATION
includes ACCESS_COARSE_LOCATION
.
Runtime request
On iOS and on Android API 23+, app also needs to request permission at runtime. Geolocation plugin handles it automatically. If the user denied location permission, location result will contain the following error:
LocationResult result = ...
result.isSuccessful // => false
result.error.type // => GeolocationResultErrorType.permissionDenied
Location result #
All geolocation API related to getting a location returns a Future<LocationResult>
.
final LocationResult result = await Geolocation.XXX;
if (result.isSuccessful) {
// location request successful, location is guaranteed to not be null
double lat = result.location.latitude;
double lng = result.location.longitude;
} else {
switch (result.error.type) {
case GeolocationResultErrorType.runtime:
// runtime error, check result.error.message
break;
case GeolocationResultErrorType.locationNotFound:
// location request did not return any result
break;
case GeolocationResultErrorType.serviceDisabled:
// location services disabled on device
// might be that GPS is turned off, or parental control (android)
break;
case GeolocationResultErrorType.permissionDenied:
// user denied location permission request
// rejection is final on iOS, and can be on Android
// user will need to manually allow the app from the settings
break;
case GeolocationResultErrorType.playServicesUnavailable:
// android only
// result.error.additionalInfo contains more details on the play services error
switch(result.error.additionalInfo as GeolocationAndroidPlayServices) {
case GeolocationAndroidPlayServices.missing:
case GeolocationAndroidPlayServices.updating:
case GeolocationAndroidPlayServices.versionUpdateRequired:
case GeolocationAndroidPlayServices.disabled:
case GeolocationAndroidPlayServices.invalid:
}
break;
}
}
Single location #
Last known location
See api documentation: [link]
LocationResult result = await Geolocation.lastKnownLocation;
Current location
See api documentation: [link]
LocationResult result = await Geolocation.currentLocation(LocationAccuracy.best);
Single location update
See api documentation: [link]
LocationResult result = await Geolocation.singleLocationUpdate(LocationAccuracy.best);