Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/loup-v/geolocation
Flutter geolocation plugin for Android and iOS.
https://github.com/loup-v/geolocation
flutter flutter-plugin geocoder geolocation gps location
Last synced: about 11 hours ago
JSON representation
Flutter geolocation plugin for Android and iOS.
- Host: GitHub
- URL: https://github.com/loup-v/geolocation
- Owner: loup-v
- License: apache-2.0
- Created: 2018-04-02T18:05:30.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-07-22T10:34:01.000Z (over 2 years ago)
- Last Synced: 2024-06-21T18:03:46.562Z (5 months ago)
- Topics: flutter, flutter-plugin, geocoder, geolocation, gps, location
- Language: Dart
- Homepage:
- Size: 554 KB
- Stars: 225
- Watchers: 16
- Forks: 96
- Open Issues: 24
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# geolocation
[![pub package](https://img.shields.io/pub/v/geolocation.svg)](https://pub.dartlang.org/packages/geolocation)
Flutter [geolocation plugin](https://pub.dartlang.org/packages/geolocation/) for Android API 16+ and iOS 9+.
Features:
- Manual and automatic location permission management
- Current one-shot location
- Continuous location updates with foreground and background optionsThe plugin is under active development and the following features are planned soon:
- Geocode
- Geofences
- Place suggestions
- Activity recognition
- Exposition of iOS/Android specific APIs (like significant location updates on iOS)| Android | iOS |
| :----------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------: |
| ![](https://github.com/loup-v/geolocation/blob/master/doc/android_screenshot.jpg?raw=true) | ![](https://github.com/loup-v/geolocation/blob/master/doc/ios_screenshot.jpg?raw=true) |### Installation
Follow the instructions: https://pub.dev/packages/geolocation#-installing-tab-
#### iOS
##### Objective-C compatibility
For Flutter projects created with the Objective-C template, you might need to add `use_frameworks!` at the top of `ios/Podfile`.
More details can be found in the following issue: https://github.com/flutter/flutter/issues/16049#issuecomment-552060349#### Android
##### AndroidX
Geolocation is dependent on AndroidX. Make sure to include the following settings to 'android/gradle.properties':
```
android.useAndroidX=true
android.enableJetifier=true
```##### R8/Proguard code obfuscation
If you have enabled code obfuscation with R8 or proguard, you need to add the following rules.
`android/app/build.gradle`:
```groovy
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
````android/app/proguard-rules.pro`:
```
# Geolocation - start-keep class app.loup.geolocation.** { *; }
# Moshi - start
# https://github.com/square/moshi/blob/master/moshi/src/main/resources/META-INF/proguard/moshi.pro# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**-keepclasseswithmembers class * {
@com.squareup.moshi.* ;
}-keep @com.squareup.moshi.JsonQualifier interface *
# Enum field names are used by the integrated EnumJsonAdapter.
# values() is synthesized by the Kotlin compiler and is used by EnumJsonAdapter indirectly
# Annotate enums with @JsonClass(generateAdapter = false) to use them with Moshi.
-keepclassmembers @com.squareup.moshi.JsonClass class * extends java.lang.Enum {
;
**[] values();
}# Moshi - end
# Geolocation - end
```### Permission
Android and iOS require to declare the location permission in a configuration file.
#### For iOS
There are two kinds of location permission available in iOS: "when in use" and "always".
If you don't know what permission to choose for your usage, see:
https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_servicesYou need to declare the description for the desired permission in `ios/Runner/Info.plist`:
```xml
NSLocationWhenInUseUsageDescription
Reason why app needs location
NSLocationAlwaysAndWhenInUseUsageDescription
Reason why app needs location
NSLocationAlwaysUsageDescription
Reason why app needs location
...```
#### For Android
There are two kinds of location permission in Android: "coarse" and "fine".
Coarse location will allow to get approximate location based on sensors like the Wifi, while fine location returns the most accurate location using GPS (in addition to coarse).You need to declare one of the two permissions in `android/app/src/main/AndroidManifest.xml`:
```xml
```
Note that `ACCESS_FINE_LOCATION` permission includes `ACCESS_COARSE_LOCATION`.
## API
For more complete documentation on all usage, check the API documentation:
https://pub.dartlang.org/documentation/geolocation/latest/geolocation/geolocation-library.htmlYou can also check the example project that showcase a comprehensive usage of Geolocation plugin.
### Check if location service is operational
API documentation: https://pub.dartlang.org/documentation/geolocation/latest/geolocation/Geolocation/isLocationOperational.html
```dart
final GeolocationResult result = await Geolocation.isLocationOperational();
if(result.isSuccessful) {
// location service is enabled, and location permission is granted
} else {
// location service is not enabled, restricted, or location permission is denied
}
```### Request location permission
On Android (api 23+) and iOS, apps need to request location permission at runtime.
_Note: You are not required to request permission manually.
Geolocation plugin will request permission automatically if it's needed, when you make a location request._API documentation: https://pub.dartlang.org/documentation/geolocation/latest/geolocation/Geolocation/requestLocationPermission.html
```dart
final GeolocationResult result = await Geolocation.requestLocationPermission(
const LocationPermission(
android: LocationPermissionAndroid.fine,
ios: LocationPermissionIOS.always,
),
openSettingsIfDenied: true,
);if(result.isSuccessful) {
// location permission is granted (or was already granted before making the request)
} else {
// location permission is not granted
// user might have denied, but it's also possible that location service is not enabled, restricted, and user never saw the permission request dialog. Check the result.error.type for details.
}
```### Get the current one-shot location
Geolocation offers three methods:
- Last known location (best on Android):
https://pub.dartlang.org/documentation/geolocation/latest/geolocation/Geolocation/lastKnownLocation.html
- Single location update (best on iOS):
https://pub.dartlang.org/documentation/geolocation/latest/geolocation/Geolocation/singleLocationUpdate.html
- Current location (best of both worlds, tries to retrieve last known location on Android, otherwise requests a single location update):
https://pub.dartlang.org/documentation/geolocation/latest/geolocation/Geolocation/currentLocation.html```dart
// get last known location, which is a future rather than a stream (best for android)
LocationResult result = await Geolocation.lastKnownLocation();// force a single location update (best for ios)
StreamSubscription subscription = Geolocation.currentLocation(accuracy: LocationAccuracy.best).listen((result) {
// todo with result
});// best option for most cases
StreamSubscription subscription = Geolocation.currentLocation(accuracy: LocationAccuracy.best).listen((result) {
if(result.isSuccessful) {
Double latitude = result.location.latitude;
// todo with result
}
});
```### Continuous location updates
API documentation: https://pub.dartlang.org/documentation/geolocation/latest/geolocation/Geolocation/locationUpdates.html
```dart
StreamSubscription subscription = Geolocation.locationUpdates(
accuracy: LocationAccuracy.best,
displacementFilter: 10.0, // in meters
inBackground: true, // by default, location updates will pause when app is inactive (in background). Set to `true` to continue updates in background.
)
.listen((result) {
if(result.isSuccessful) {
// todo with result
}
});// cancelling subscription will also stop the ongoing location request
subscription.cancel();
```### Handle location result
Location request return either a `LocationResult` future or a stream of `LocationResult`.
API documentation: https://pub.dartlang.org/documentation/geolocation/latest/geolocation/LocationResult-class.html
```dart
LocationResult result = await Geolocation.lastKnownLocation();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.permissionNotGranted:
// location has not been requested yet
// app must request permission in order to access the location
break;
case GeolocationResultErrorType.permissionDenied:
// user denied the location permission for the app
// rejection is final on iOS, and can be on Android if user checks `don't ask again`
// user will need to manually allow the app from the settings, see requestLocationPermission(openSettingsIfDenied: true)
break;
case GeolocationResultErrorType.playServicesUnavailable:
// android only
// result.error.additionalInfo contains more details on the play services error
switch(result.error.additionalInfo as GeolocationAndroidPlayServices) {
// do something, like showing a dialog inviting the user to install/update play services
case GeolocationAndroidPlayServices.missing:
case GeolocationAndroidPlayServices.updating:
case GeolocationAndroidPlayServices.versionUpdateRequired:
case GeolocationAndroidPlayServices.disabled:
case GeolocationAndroidPlayServices.invalid:
}
break;
}
}
```## Authors
Geolocation plugin is developed by Loup, a mobile development studio based in Montreal and Paris.
You can contact us at## Contributers
- lukaspili
- mit-mit
- shehabic-work
- Abgaryan
- shehabic
- alfanhui## License
Apache License 2.0