GithubHelp home page GithubHelp logo

bhanditz / android-reactive-location-library Goto Github PK

View Code? Open in Web Editor NEW

This project forked from filipelipan/android-reactive-location-library

0.0 2.0 0.0 123 KB

License: MIT License

Java 11.95% Kotlin 88.05%

android-reactive-location-library's Introduction

reactive-location

A simple RxJava wrapper for location requests in Android written in Kotlin

Features


  • Using FusedLocationApi from Google Play Services.
  • Location events will be delivered by stream via RxJava.
  • It'll have a single stream for each LocationRequest.
  • Support for runtime Location Permission and Location Settings errors.
  • Simple errors resolution with startActivityForSolution for both Permission and Settings errors.
  • Google Play Services disconnect when all requests are no longer being observed.

Usage

val REQUEST_CODE_LOCATION_EXCEPTION = 1
var subscription: Subscription? = null

override fun onStart() {
  super.onStart()
  startListeningForLocations()
}

override fun onStop() {
  super.onStop()
  subscription?.unsubscribe()
}

private fun startListeningForLocations() {
  val locationApiProvider = FusedLocationApiProvider()
  val locationManager = LocationManager(locationApiProvider)
  val locationRequest = LocationRequest.create()
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
        .setInterval(1000)

  subscription = locationManager.streamForRequest(this, locationRequest).subscribe(
    { location ->
        // use locations as pleased
    },
    { error ->
       // Check if the error has solution. Settings or Permission exceptions
       if (error is LocationConnectionException && error.hasSolution()) {
         error.startActivityForSolution(this, REQUEST_CODE_LOCATION_EXCEPTION)
       }
    }
  )
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
  super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  if (requestCode == REQUEST_CODE_LOCATION_EXCEPTION && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
      startListeningToLocation()
  }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {  super.onActivityResult(requestCode, resultCode, data)
  if (requestCode == REQUEST_CODE_LOCATION_EXCEPTION && resultCode == Activity.RESULT_OK) {
    startListeningToLocation()
  }
}

Download


Grab via Maven:

<dependency>
  <groupId>com.ninenine.reactivelocation</groupId>
  <artifactId>reactive-location</artifactId>
  <version>0.0.2</version>
  <type>pom</type>
</dependency>

or Gradle:

repositories {
  maven { url 'http://dl.bintray.com/99/android' }
}

compile 'com.ninenine.reactivelocation:reactive-location:0.0.2'

Reactive-location requires at minimum Android 2.3.

android-reactive-location-library's People

Contributors

philipesteiff avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.