{"id":13571518,"url":"https://github.com/mcharmas/Android-ReactiveLocation","last_synced_at":"2025-04-04T08:31:20.332Z","repository":{"id":13622729,"uuid":"16315976","full_name":"mcharmas/Android-ReactiveLocation","owner":"mcharmas","description":"Small library that wraps Google Play Service API in brilliant RxJava Observables reducing boilerplate to minimum.","archived":false,"fork":false,"pushed_at":"2024-07-18T14:24:40.000Z","size":647,"stargazers_count":2103,"open_issues_count":33,"forks_count":314,"subscribers_count":75,"default_branch":"master","last_synced_at":"2025-04-03T14:57:34.324Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcharmas.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-01-28T15:37:47.000Z","updated_at":"2025-04-01T06:29:11.000Z","dependencies_parsed_at":"2022-09-16T11:32:21.451Z","dependency_job_id":"09766f47-651e-4e1f-a216-4975e3f540a2","html_url":"https://github.com/mcharmas/Android-ReactiveLocation","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcharmas%2FAndroid-ReactiveLocation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcharmas%2FAndroid-ReactiveLocation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcharmas%2FAndroid-ReactiveLocation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcharmas%2FAndroid-ReactiveLocation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcharmas","download_url":"https://codeload.github.com/mcharmas/Android-ReactiveLocation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247147219,"owners_count":20891648,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T14:01:02.730Z","updated_at":"2025-04-04T08:31:20.304Z","avatar_url":"https://github.com/mcharmas.png","language":"Java","readme":"ReactiveLocation library for Android\n====================================\n\nSmall library that wraps Google Play Services API in brilliant [RxJava](https://github.com/ReactiveX/RxJava)\n```Observables``` reducing boilerplate to minimum.\n\nCurrent stable version - 2.1\n---------------\n\n**This version works with Google Play Services 11+ and RxJava 2.+**\n\nArtifact name: ```android-reactive-location2```\n\nRxJava1 stable version - 1.0\n--------------\n**RxJava1 version:**\n\nArtifact name: ```android-reactive-location```\n\nWhat can you do with that?\n--------------------------\n\n* easily connect to Play Services API\n* obtain last known location\n* subscribe for location updates\n* use location settings API\n* manage geofences\n* geocode location to list of addresses\n* activity recognition\n* use current place API\n* fetch place autocomplete suggestions\n\nHow does the API look like?\n----------------------------\n\nSimple. All you need is to create ```ReactiveLocationProvider``` using your context.\nAll observables are already there. Examples are worth more than 1000 words:\n\n\n### Getting last known location\n\n```java\nReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);\nlocationProvider.getLastKnownLocation()\n    .subscribe(new Consumer\u003cLocation\u003e() {\n        @Override\n        public void call(Location location) {\n            doSthImportantWithObtainedLocation(location);\n        }\n    });\n```\n\nYep, Java 8 is not there yet (and on Android it will take a while) but there is\nabsolutely no Google Play Services LocationClient callbacks hell and there is no\nclean-up you have to do.\n\n### Subscribing for location updates\n\n```java\nLocationRequest request = LocationRequest.create() //standard GMS LocationRequest\n                                  .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)\n                                  .setNumUpdates(5)\n                                  .setInterval(100);\n\nReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);\nSubscription subscription = locationProvider.getUpdatedLocation(request)\n    .filter(...)    // you can filter location updates\n    .map(...)       // you can map location to sth different\n    .flatMap(...)   // or event flat map\n    ...             // and do everything else that is provided by RxJava\n    .subscribe(new Consumer\u003cLocation\u003e() {\n        @Override\n        public void call(Location location) {\n            doSthImportantWithObtainedLocation(location);\n        }\n    });\n```\n\nWhen you are done (for example in ```onStop()```) remember to unsubscribe.\n\n```java\nsubscription.unsubscribe();\n```\n\n### Subscribing for Activity Recognition\n\nGetting activity recognition is just as simple\n\n```java\n\nReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);\nSubscription subscription = locationProvider.getDetectedActivity(0) // detectionIntervalMillis\n    .filter(...)    // you can filter location updates\n    .map(...)       // you can map location to sth different\n    .flatMap(...)   // or event flat map\n    ...             // and do everything else that is provided by RxJava\n    .subscribe(new Consumer\u003cActivityRecognitionResult\u003e() {\n        @Override\n        public void call(ActivityRecognitionResult detectedActivity) {\n            doSthImportantWithObtainedActivity(detectedActivity);\n        }\n    });\n```\n\n### Reverse geocode location\n\nDo you need address for location?\n\n```java\nObservable\u003cList\u003cAddress\u003e\u003e reverseGeocodeObservable = locationProvider\n    .getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), MAX_ADDRESSES);\n\nreverseGeocodeObservable\n    .subscribeOn(Schedulers.io())               // use I/O thread to query for addresses\n    .observeOn(AndroidSchedulers.mainThread())  // return result in main android thread to manipulate UI\n    .subscribe(...);\n```\n\n### Geocode location\n\nDo you need address for a text search query?\n\n```java\nObservable\u003cList\u003cAddress\u003e\u003e geocodeObservable = locationProvider\n    .getGeocodeObservable(String userQuery, MAX_ADDRESSES);\n\ngeocodeObservable\n    .subscribeOn(Schedulers.io())\n    .observeOn(AndroidSchedulers.mainThread())\n    .subscribe(...);\n```\n\n### Managing geofences\n\nFor geofence management use `addGeofences` and `removeGeofences` methods.\n\n### Checking location settings though location settings API\n\nTo get ```LocationSettingsResponse``` for your ```LocationRequest``` check\nout ```ReactiveLocationProvider.checkLocationSettings()``` method. Sample\nusage can be found in **sample** project in ```MainActivity``` class.\n\n### Connecting to Google Play Services API\n\nIf you just need managed connection to Play Services API\nuse ```ReactiveLocationProvider.getGoogleApiClientObservable()```.\nOn subscription it will connect to the API.\nUnsubscription will close the connection.\n\n### Creating observable from PendingResult\n\nIf you are manually using Google Play Services and you are dealing with\n```PendingResult``` you can easily transform them to observables with\n```ReactiveLocationProvider.fromPendingResult()``` method.\n\n### Transforming buffers to observable\n\nTo transform any buffer to observable and autorelease it on unsubscription\nuse ```DataBufferObservable.from()``` method. It will let you easily flatMap\nsuch data as ```PlaceLikelihoodBuffer``` or ```AutocompletePredictionBuffer```\nfrom Places API. For usage example see ```PlacesActivity``` sample.\n\n### Places API\n\nYou can fetch current place or place suggestions using:\n\n* ```ReactiveLocationProvider.getCurrentPlace()```\n* ```ReactiveLocationProvider.getPlaceAutocompletePredictions()```\n* ```ReactiveLocationProvider.getPlaceById()```\n\nFor more info see sample project and ```PlacesActivity```.\n\n### Cooler examples\n\nDo you need location with certain accuracy but don't want to wait for it more than 4 sec? No problem.\n\n```java\nLocationRequest req = LocationRequest.create()\n                         .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)\n                         .setExpirationDuration(TimeUnit.SECONDS.toMillis(LOCATION_TIMEOUT_IN_SECONDS))\n                         .setInterval(LOCATION_UPDATE_INTERVAL);\n\nObservable\u003cLocation\u003e goodEnoughQuicklyOrNothingObservable = locationProvider.getUpdatedLocation(req)\n            .filter(new Func1\u003cLocation, Boolean\u003e() {\n                @Override\n                public Boolean call(Location location) {\n                    return location.getAccuracy() \u003c SUFFICIENT_ACCURACY;\n                }\n            })\n            .timeout(LOCATION_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS, Observable.just((Location) null), AndroidSchedulers.mainThread())\n            .first()\n            .observeOn(AndroidSchedulers.mainThread());\n\ngoodEnoughQuicklyOrNothingObservable.subscribe(...);\n```\n\n\nHow to use it?\n--------------\n\nLibrary is available in maven central.\n\n### Gradle\n\nJust use it as dependency in your *build.gradle* file\nalong with Google Play Services and RxJava.\n\n```groovy\ndependencies {\n    ...\n    compile 'pl.charmas.android:android-reactive-location2:2.1@aar'\n    compile 'com.google.android.gms:play-services-location:11.0.4' //you can use newer GMS version if you need\n    compile 'com.google.android.gms:play-services-places:11.0.4'\n    compile 'io.reactivex:rxjava:2.0.5' //you can override RxJava version if you need\n}\n```\n\n### Maven\n\nEnsure you have android-maven-plugin version that support **aar** archives and add\nfollowing dependency:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003epl.charmas.android\u003c/groupId\u003e\n    \u003cartifactId\u003eandroid-reactive-location2\u003c/artifactId\u003e\n    \u003cversion\u003e2.1\u003c/version\u003e\n    \u003ctype\u003eaar\u003c/type\u003e\n\u003c/dependency\u003e\n```\n\nIt may be necessary to add google play services and rxanroid dependency as well.\n\nSample\n------\n\nSample usage is available in *sample* directory.\n\nPlaces API requires API Key. Before running samples you need to create project on API console\nand obtain API Key using this [guide](https://developers.google.com/places/android/signup).\nObtained key should be exported as gradle property named: ```REACTIVE_LOCATION_GMS_API_KEY``` for\nexample in ```~/.gradle/gradle.properties```.\n\n\nReferences\n------\n\nIf you need Google Fit library rxified please take a look at [RxFit](https://github.com/patloew/RxFit).\n\nLicense\n=======\n\n    Copyright (C) 2015 Michał Charmas (http://blog.charmas.pl)\n\n\tLicensed under the Apache License, Version 2.0 (the \"License\");\n\tyou may not use this file except in compliance with the License.\n\tYou may obtain a copy of the License at\n\n\t     http://www.apache.org/licenses/LICENSE-2.0\n\n\tUnless required by applicable law or agreed to in writing, software\n\tdistributed under the License is distributed on an \"AS IS\" BASIS,\n\tWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\tSee the License for the specific language governing permissions and\n\tlimitations under the License.\n","funding_links":[],"categories":["Library","Java","Libs","Rx","Bindings"],"sub_categories":["一些原理分析的文章","\u003cA NAME=\"SDK\"\u003e\u003c/A\u003eSDK"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcharmas%2FAndroid-ReactiveLocation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcharmas%2FAndroid-ReactiveLocation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcharmas%2FAndroid-ReactiveLocation/lists"}