All Projects → psteiger → LocationFetcher

psteiger / LocationFetcher

Licence: MIT license
Easy Location fetching for Android apps.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to LocationFetcher

FusedBulb
Location fetch library.
Stars: ✭ 22 (-33.33%)
Mutual labels:  location, location-picker
Sppermissions
Ask permissions with ready-use interface. You can check status permission and if it has been requested before. Support SwiftUI.
Stars: ✭ 4,701 (+14145.45%)
Mutual labels:  location, permissions
CustomPermissionsDialogue
Custom Permissions Dialogue is the only permissions library that supports ALL permission request scenarios. This library handles multiple edge cases such as not enabling all permissions or permanently rejecting a permission request.
Stars: ✭ 51 (+54.55%)
Mutual labels:  permissions, dialogs
android
Where you can find everything Android from Mapzen
Stars: ✭ 106 (+221.21%)
Mutual labels:  location, location-picker
gps-share
Utility to share your GPS device on local network
Stars: ✭ 49 (+48.48%)
Mutual labels:  location
cordova-spotify-oauth
🔐 Easy Spotify authentication for Cordova / PhoneGap apps
Stars: ✭ 15 (-54.55%)
Mutual labels:  android-lib
google streetview
A command line tool and module for Google Street View Image API
Stars: ✭ 77 (+133.33%)
Mutual labels:  location
hms-location-demo
HUAWEI Location Kit sample code encapsulates APIs of the HUAWEI Location Kit. It provides many sample programs for your reference or usage.
Stars: ✭ 32 (-3.03%)
Mutual labels:  location
commons
flutter commons package
Stars: ✭ 42 (+27.27%)
Mutual labels:  dialogs
TextHighlighter
TextHighlighter is a simple android library to show highlighted and styled text in android apps.
Stars: ✭ 25 (-24.24%)
Mutual labels:  android-apps
HijriDatePicker
Material (Gregorian - Hijri) Date & Time Picker
Stars: ✭ 128 (+287.88%)
Mutual labels:  dialogs
InstagramLocationScraper
No description or website provided.
Stars: ✭ 13 (-60.61%)
Mutual labels:  location
django-improved-permissions
Django application made to make django's default permission system more robust.
Stars: ✭ 14 (-57.58%)
Mutual labels:  permissions
locationestimatr.github.io
This is a game where you are put anywhere on earth, and you have to figure out where you are
Stars: ✭ 46 (+39.39%)
Mutual labels:  location
fastapi-auth0
FastAPI authentication and authorization using auth0.com
Stars: ✭ 104 (+215.15%)
Mutual labels:  permissions
database-adapter
Database adapter for PHP-Casbin, Casbin is a powerful and efficient open-source access control library.
Stars: ✭ 21 (-36.36%)
Mutual labels:  permissions
applivery-android-sdk
Applivery Android SDK
Stars: ✭ 19 (-42.42%)
Mutual labels:  android-apps
TimeZoneLocate
Time zone for locations offline in Swift (iOS).
Stars: ✭ 30 (-9.09%)
Mutual labels:  location
kakoune-sudo-write
Write to files using 'sudo'
Stars: ✭ 24 (-27.27%)
Mutual labels:  permissions
WSA-GA-Actions
Automated Script to bake WSA Packages with GApps, All that in just one click!
Stars: ✭ 18 (-45.45%)
Mutual labels:  android-apps

LocationFetcher

Download

⚠️ If you're using Jetpack Compose, see this README instead.

Simple location fetcher for Android Apps built with Kotlin and Coroutines.

Building location-aware Android apps can be a bit tricky. This library makes it as simple as:

class MyActivity : ComponentActivity() {

    private val locationFetcher = locationFetcher({ getString(R.string.location_rationale) }) {
        interval = 5.seconds
        priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        locationFetcher.location
            .onEach { errorsOrLocation ->
                errorsOrLocation.tap { location ->
                    // Got location
                }.tapLeft { errors ->
                    // Handle errors (no permission/location settings disabled).
                    // Note that this library will automatically try to resolve errors.
                }
            }
            .flowWithLifecycle(Lifecycle.State.STARTED)
            .launchIn(this)
    }
}

This library provides a simple location component, LocationFetcher, requiring only an instance of either ComponentActivity, Fragment or Context, to make your Android app location-aware.

If the device's location services are disabled, or if your app is not allowed location permissions by the user, this library will automatically ask the user to enable location services in settings or to allow the necessary permissions as soon as you start collecting the LocationFetcher.location flow.

The service uses GPS and network as location providers by default and thus the app needs to declare use of the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions on its AndroidManifest.xml. Those permissions are already declared in this library, so manifest merging takes care of it.

You can personalize your LocationRequest to suit your needs using the custom configuration block.

Installation with Gradle

Setup Maven Central on project-level build.gradle

This library is hosted in Maven Central, so you must set it up for your project before adding the module-level dependency.

New way

The new way to install dependencies repositories is through the dependencyResolutionManagement DSL in settings.gradle(.kts).

Kotlin or Groovy:

dependencyResolutionManagement {
    repositories {
        mavenCentral()
    }
}

OR

Old way

On project-level build.gradle:

Kotlin or Groovy:

allprojects {
  repositories {
    mavenCentral()
  }
}

Add dependency

On app-level build.gradle, add dependency:

dependencies {
  implementation("app.freel:locationfetcher:9.0.0")
}

Usage

Instantiating

On any ComponentActivity, Fragment, or Context class, you can instantiate a LocationFetcher by calling the extension functions on ComponentActivity, Fragmnet, or Context:

locationFetcher({ getString(R.string.location_rationale) }) {
    // configuration block
}

Alternatively, there are some LocationFetcher() method overloads. You can see all public builders in here.

If LocationFetcher is created with a ComponentActivity or Fragment, it will be able to show dialogs to request the user to enable permission in Android settings and to allow the app to obtain the device's location. If LocationFetcher is created with a non-UI Context, it won't be able to show dialogs.

Permission rationale

In accordance with Google's best practices and policies, if user denies location permission, we must tell the user the rationale for the need of the user location, then we can ask permission a last time. If denied again, we must respect the user's decision.

The rationale must be passed to LocationFetcher builders. It will be shown to the user as an AlertDialog.

Collecting location

Once instantiated, the component gives you three Flows to collect: one for new locations, one for settings status, and one for location permissions status. Usually, you only need to collect the location flow, as errors also flow through it already.

val LocationFetcher.location: SharedFlow<Either<Nel<LocationFetcher.Error>, Location>> // Nel stands for non-empty list.
val LocationFetcher.permissionStatus: SharedFlow<Boolean>
val LocationFetcher.settingsStatus: SharedFlow<Boolean>

To manually request location permissions or location settings enablement, you can call the following APIs:

suspend fun requestLocationPermissions()
suspend fun requestEnableLocationSettings()

Results will be delivered on the aforementioned flows.

Options

LocationFetcher supports the following configurations for location fetching when creating the component:

(Note: for GPS and Network providers, only interval and smallestDisplacement are used. If you want to use all options, limit providers to LocationRequest.Provider.Fused, which is the default)

locationFetcher("We need your permission to use your location for showing nearby items") {
    fastestInterval = 5.seconds
    interval = 15.seconds
    maxWaitTime = 2.minutes
    priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    smallestDisplacement = 50f
    isWaitForAccurateLocation = false
    providers = listOf(
        LocationRequest.Provider.GPS,
        LocationRequest.Provider.Network, 
        LocationRequest.Provider.Fused
    )
    numUpdates = Int.MAX_VALUE
    debug = false
}

Alternatively, you might prefer to create a standalone configuration instance. It is useful, for example, when sharing a common configuration between multiple LocationFetcher instances:

val config = LocationFetcher.Config(
    rationale = "We need your permission to use your location for showing nearby items",
    fastestInterval = 5.seconds,
    interval = 15.seconds,
    maxWaitTime = 2.minutes,
    priority = LocationRequest.PRIORITY_HIGH_ACCURACY,
    smallestDisplacement = 50f,
    isWaitForAccurateLocation = false,
    providers = listOf(
        LocationRequest.Provider.GPS,
        LocationRequest.Provider.Network,
        LocationRequest.Provider.Fused
    ),
    numUpdates = Int.MAX_VALUE,
    debug = true
)
val locationFetcher = locationFetcher(config)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].