{"id":14973086,"url":"https://github.com/firebase/geofire-android","last_synced_at":"2025-10-19T11:30:18.016Z","repository":{"id":36567435,"uuid":"191675850","full_name":"firebase/geofire-android","owner":"firebase","description":"GeoFire for Android apps","archived":false,"fork":false,"pushed_at":"2023-09-15T17:37:09.000Z","size":268,"stargazers_count":133,"open_issues_count":4,"forks_count":36,"subscribers_count":52,"default_branch":"master","last_synced_at":"2024-09-28T17:01:37.527Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/firebase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","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":"2019-06-13T02:23:05.000Z","updated_at":"2024-08-27T07:21:48.000Z","dependencies_parsed_at":"2024-09-23T12:41:19.102Z","dependency_job_id":null,"html_url":"https://github.com/firebase/geofire-android","commit_stats":{"total_commits":23,"total_committers":4,"mean_commits":5.75,"dds":"0.21739130434782605","last_synced_commit":"85e8c220ef7f5024a11f40e88540f5a740006273"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firebase%2Fgeofire-android","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firebase%2Fgeofire-android/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firebase%2Fgeofire-android/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firebase%2Fgeofire-android/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/firebase","download_url":"https://codeload.github.com/firebase/geofire-android/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219869249,"owners_count":16555572,"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-09-24T13:48:05.615Z","updated_at":"2025-10-19T11:30:17.503Z","avatar_url":"https://github.com/firebase.png","language":"Java","readme":"# GeoFire for Android — Realtime location queries with Firebase\n\n[![Actions Status][gh-actions-badge]][gh-actions]\n\nGeoFire is an open-source library for Android that allows you to store and query a\nset of keys based on their geographic location.\n\nAt its heart, GeoFire simply stores locations with string keys. Its main\nbenefit however, is the possibility of querying keys within a given geographic\narea - all in realtime.\n\nGeoFire uses the [Firebase Realtime Database](https://firebase.google.com/products/realtime-database/) for\ndata storage, allowing query results to be updated in realtime as they change.\nGeoFire *selectively loads only the data near certain locations, keeping your\napplications light and responsive*, even with extremely large datasets.\n\nGeoFire clients are also available for other languages:\n\n  * [Objective-C (iOS)](https://github.com/firebase/geofire-objc)\n  * [JavaScript (Web)](https://github.com/firebase/geofire-js)\n  * [Java (Server)](https://github.com/firebase/geofire-java)\n\n### Integrating GeoFire with your data\n\nGeoFire is designed as a lightweight add-on to the Firebase Realtime Database. However, to keep things\nsimple, GeoFire stores data in its own format and its own location within\nyour Firebase database. This allows your existing data format and security rules to\nremain unchanged and for you to add GeoFire as an easy solution for geo queries\nwithout modifying your existing data.\n\n### Example Usage\n\nAssume you are building an app to rate bars and you store all information for a\nbar, e.g. name, business hours and price range, at `/bars/\u003cbar-id\u003e`. Later, you\nwant to add the possibility for users to search for bars in their vicinity. This\nis where GeoFire comes in. You can store the location for each bar using\nGeoFire, using the bar IDs as GeoFire keys. GeoFire then allows you to easily\nquery which bar IDs (the keys) are nearby. To display any additional information\nabout the bars, you can load the information for each bar returned by the query\nat `/bars/\u003cbar-id\u003e`.\n\n## Including GeoFire in your Android project\n\nIn order to use GeoFire in your project, you need to [add the Firebase Android\nSDK](https://firebase.google.com/docs/android/setup). After that you can include GeoFire with one of the choices below.\n\nAdd a dependency for GeoFire to your app's `build.gradle` file.\n\n```groovy\ndependencies {\n    // Full GeoFire library for Realtime Database users\n    implementation 'com.firebase:geofire-android:3.2.0'\n\n    // GeoFire utililty functions for Cloud Firestore users who\n    // want to implement their own geo solution, see:\n    // https://firebase.google.com/docs/firestore/solutions/geoqueries\n    implementation 'com.firebase:geofire-android-common:3.2.0'\n}\n```\n\n## Usage\n\nThere are two ways to use GeoFire:\n\n  - GeoFire - an end-to-end solution for adding simple geo queries to apps using Firebase Realtime Database.\n  - GeoFireUtils - a set of utilities that make it simple to build a geo query solution for any app, such as those using Cloud Firestore.\n\n### GeoFire\n\nA `GeoFire` object is used to read and write geo location data to your Firebase\ndatabase and to create queries. To create a new `GeoFire` instance you need to attach it to a Firebase database\nreference.\n\n```java\nDatabaseReference ref = FirebaseDatabase.getInstance().getReference(\"path/to/geofire\");\nGeoFire geoFire = new GeoFire(ref);\n```\n\nNote that you can point your reference to anywhere in your Firebase database, but don't\nforget to [setup security rules for\nGeoFire](https://github.com/firebase/geofire-js/blob/master/examples/securityRules).\n\n#### Setting location data\n\nIn GeoFire you can set and query locations by string keys. To set a location for\na key simply call the `setLocation` method. The method is passed a key\nas a string and the location as a `GeoLocation` object containing the location's latitude and longitude:\n\n```java\ngeoFire.setLocation(\"firebase-hq\", new GeoLocation(37.7853889, -122.4056973));\n```\n\nTo check if a write was successfully saved on the server, you can add a\n`GeoFire.CompletionListener` to the `setLocation` call:\n\n```java\ngeoFire.setLocation(\"firebase-hq\", new GeoLocation(37.7853889, -122.4056973), new GeoFire.CompletionListener() {\n    @Override\n    public void onComplete(String key, FirebaseError error) {\n        if (error != null) {\n            System.err.println(\"There was an error saving the location to GeoFire: \" + error);\n        } else {\n            System.out.println(\"Location saved on server successfully!\");\n        }\n    }\n});\n```\n\nTo remove a location and delete it from the database simply pass the location's key to `removeLocation`:\n\n```java\ngeoFire.removeLocation(\"firebase-hq\");\n```\n\n#### Retrieving a location\n\nRetrieving a location for a single key in GeoFire happens with callbacks:\n\n```java\ngeoFire.getLocation(\"firebase-hq\", new LocationCallback() {\n    @Override\n    public void onLocationResult(String key, GeoLocation location) {\n        if (location != null) {\n            System.out.println(String.format(\"The location for key %s is [%f,%f]\", key, location.latitude, location.longitude));\n        } else {\n            System.out.println(String.format(\"There is no location for key %s in GeoFire\", key));\n        }\n    }\n\n    @Override\n    public void onCancelled(DatabaseError databaseError) {\n        System.err.println(\"There was an error getting the GeoFire location: \" + databaseError);\n    }\n});\n```\n\n### Geo Queries\n\nGeoFire allows you to query all keys within a geographic area using `GeoQuery`\nobjects. As the locations for keys change, the query is updated in realtime and fires events\nletting you know if any relevant keys have moved. `GeoQuery` parameters can be updated\nlater to change the size and center of the queried area.\n\n```java\n// creates a new query around [37.7832, -122.4056] with a radius of 0.6 kilometers\nGeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(37.7832, -122.4056), 0.6);\n```\n\n#### Receiving events for geo queries\n\n##### Key Events\n\nThere are five kinds of \"key\" events that can occur with a geo query:\n\n1. **Key Entered**: The location of a key now matches the query criteria.\n2. **Key Exited**: The location of a key no longer matches the query criteria.\n3. **Key Moved**: The location of a key changed but the location still matches the query criteria.\n4. **Query Ready**: All current data has been loaded from the server and all\n   initial events have been fired.\n5. **Query Error**: There was an error while performing this query, e.g. a\n   violation of security rules.\n\nKey entered events will be fired for all keys initially matching the query as well as any time\nafterwards that a key enters the query. Key moved and key exited events are guaranteed to be\npreceded by a key entered event.\n\nSometimes you want to know when the data for all the initial keys has been\nloaded from the server and the corresponding events for those keys have been\nfired. For example, you may want to hide a loading animation after your data has\nfully loaded. This is what the \"ready\" event is used for.\n\nNote that locations might change while initially loading the data and key moved and key\nexited events might therefore still occur before the ready event is fired.\n\nWhen the query criteria is updated, the existing locations are re-queried and the\nready event is fired again once all events for the updated query have been\nfired. This includes key exited events for keys that no longer match the query.\n\nTo listen for events you must add a `GeoQueryEventListener` to the `GeoQuery`:\n\n```java\ngeoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {\n    @Override\n    public void onKeyEntered(String key, GeoLocation location) {\n        System.out.println(String.format(\"Key %s entered the search area at [%f,%f]\", key, location.latitude, location.longitude));\n    }\n\n    @Override\n    public void onKeyExited(String key) {\n        System.out.println(String.format(\"Key %s is no longer in the search area\", key));\n    }\n\n    @Override\n    public void onKeyMoved(String key, GeoLocation location) {\n        System.out.println(String.format(\"Key %s moved within the search area to [%f,%f]\", key, location.latitude, location.longitude));\n    }\n\n    @Override\n    public void onGeoQueryReady() {\n        System.out.println(\"All initial data has been loaded and events have been fired!\");\n    }\n\n    @Override\n    public void onGeoQueryError(DatabaseError error) {\n        System.err.println(\"There was an error with this query: \" + error);\n    }\n});\n```\n\nYou can call either `removeGeoQueryEventListener` to remove a\nsingle event listener or `removeAllListeners` to remove all event listeners\nfor a `GeoQuery`.\n\n##### Data Events\n\nIf you are storing model data and geo data in the same database location, you may\nwant access to the `DataSnapshot` as part of geo events. In this case, use a\n`GeoQueryDataEventListener` rather than a key listener.\n\nThese \"data event\" listeners have all of the same events as the key listeners with\none additional event type:\n\n  6. **Data Changed**: the underlying `DataSnapshot` has changed. Every \"data moved\"\n     event is followed by a data changed event but you can also get change events without\n     a move if the data changed does not affect the location.\n\nAdding a data event listener is similar to adding a key event listener:\n\n```java\ngeoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {\n\n  @Override\n  public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {\n    // ...\n  }\n\n  @Override\n  public void onDataExited(DataSnapshot dataSnapshot) {\n    // ...\n  }\n\n  @Override\n  public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {\n    // ...\n  }\n\n  @Override\n  public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {\n    // ...\n  }\n\n  @Override\n  public void onGeoQueryReady() {\n    // ...\n  }\n\n  @Override\n  public void onGeoQueryError(DatabaseError error) {\n    // ...\n  }\n\n});\n\n```\n\n#### Updating the query criteria\n\nThe `GeoQuery` search area can be changed with `setCenter` and `setRadius`. Key\nexited and key entered events will be fired for keys moving in and out of\nthe old and new search area, respectively. No key moved events will be\nfired; however, key moved events might occur independently.\n\nUpdating the search area can be helpful in cases such as when you need to update\nthe query to the new visible map area after a user scrolls.\n\n### GeoFireUtils\n\nThe `geofire-android-common` library provides the `GeoFireUtils` class which contains utilities for working with geohashes but has no dependency on or integration with a specific database. The `GeoFireUtils` class contains the following utility methods:\n\n  * `String getGeoHashForLocation(@NonNull GeoLocation location)` - compute the geohash string for a given (lat,lng) par with default precision.\n  * `String getGeoHashForLocation(@NonNull GeoLocation location, int precision)` - compute the geohash string for a given (lat, lng) pair with custom precision.\n  * `double getDistanceBetween(@NonNull GeoLocation a, @NonNull GeoLocation b)` - compute the distance, in kilometers, between two locations.\n  * `List\u003cGeoQueryBounds\u003e getGeoHashQueryBounds(@NonNull GeoLocation location, double radius)` - given a center point and a radius distance, compute a set of query bounds that can be joined to find all points within the radius distance of the center.\n\nFor a detailed guide on how to use these utilities to add geo querying capabilities to your Cloud Firestore app, see: https://firebase.google.com/docs/firestore/solutions/geoqueries\n\n## Publishing\n\n### Versioning\n\nWe use [SemVer](https://semver.org/) in this project.\n\nWhen you bump a version, be sure to update:\n\n- [common/gradle.properties](common/gradle.properties)\n- [library/gradle.properties](library/gradle.properties)\n- [README.md](README.md)\n\n### Credentials\n\nThe library is published to Maven Central by the firebase-sonatype account, Googlers can find the\npassword for this account in [Valentine](http://valentine/)\n\n### GPG Key\n\nYou will need to create a private GPG keyring on your machine, if you don't have one do the\nfollowing steps:\n\n  1. Run `gpg --full-generate-key`\n  1. Choose `RSA and RSA` for the key type\n  1. Use `4096` for the key size\n  1. Use `0` for the expiration (never)\n  1. Use any name, email address, and password\n  \nThis creates your key in `~/.gnupg/openpgp-revocs.d/` with `.rev` format. The last 8 characters\nbefore the `.rev` extension are your **Key ID**.\n\nTo export the key, run:\n\n```\ngpg --export-secret-keys -o $HOME/sonatype.gpg\n```\n\nFinally upload your key to the keyserver:\n\n```\ngpg --keyserver hkp://keys.openpgp.org --send-keys \u003cYOUR KEY ID\u003e\n```\n\n### Local Properties\n\nOpen your `$HOME/.gradle/gradle.properties` file at and fill in the values:\n\n```\nsigning.keyId=\u003cKEY ID\u003e\nsigning.password=\u003cPASSWORD YOU CHOSE\u003e\nsigning.secretKeyRingFile=\u003cFULL PATH TO YOUR GPG FILE\u003e\nmavenCentralRepositoryUsername=firebase-sonatype\nmavenCentralRepositoryPassword=\u003cPASSWORD FROM VALENTINE\u003e\n```\n\n### Publish\n\nTo publish, run:\n\n```\n./gradlew publish\n```\n\n### Release\n\nFollow [the instructions here](https://central.sonatype.org/pages/releasing-the-deployment.html):\n\n  1. Navigate to https://oss.sonatype.org/ and **Log In**\n  1. On the left side menu, click **Staging Repositories** (under **Build Promotion**) and look for the `com.firebase` repo\n  1. You should see it with the `Open` status. Click **Close** and wait a few minutes (you can check status by clicking **Refresh**)\n  1. Once the status changes to `Closed`, click **Release**\n\n[gh-actions]: https://github.com/firebase/geofire-android/actions\n[gh-actions-badge]: https://github.com/firebase/geofire-android/workflows/Android%20CI/badge.svg\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirebase%2Fgeofire-android","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffirebase%2Fgeofire-android","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirebase%2Fgeofire-android/lists"}