GeoFire for Android/Java


Source link: https://github.com/firebase/geofire-java

GeoFire for Android/Java — Realtime location queries with Firebase

GeoFire is an open-source library for Android/Java that allows you to store and query a set of keys based on their geographic location.

At its heart, GeoFire simply stores locations with string keys. Its main benefit however, is the possibility of querying keys within a given geographic area - all in realtime.

GeoFire uses the Firebase database for data storage, allowing query results to be updated in realtime as they change. GeoFire selectively loads only the data near certain locations, keeping your applications light and responsive, even with extremely large datasets.

A compatible GeoFire client is also available for Objective-C and JavaScript.

For a full example of an application using GeoFire to display realtime transit data, see the SFVehicles example in Android app in this repo.

Integrating GeoFire with your data

GeoFire is designed as a lightweight add-on to the Firebase Realtime Database. However, to keep things simple, GeoFire stores data in its own format and its own location within your Firebase database. This allows your existing data format and security rules to remain unchanged and for you to add GeoFire as an easy solution for geo queries without modifying your existing data.

Example Usage

Assume you are building an app to rate bars and you store all information for a bar, e.g. name, business hours and price range, at /bars/<bar-id>. Later, you want to add the possibility for users to search for bars in their vicinity. This is where GeoFire comes in. You can store the location for each bar using GeoFire, using the bar IDs as GeoFire keys. GeoFire then allows you to easily query which bar IDs (the keys) are nearby. To display any additional information about the bars, you can load the information for each bar returned by the query at /bars/<bar-id>.

Upgrading GeoFire

Upgrading from GeoFire 1.x to 2.x

GeoFire 2.x is based on the new 3.x release of Firebase.

Upgrading from GeoFire 1.0.x to 1.1.x

With the release of GeoFire for Android/Java 1.1.0, this library now uses the new query functionality found in Firebase 2.0.0. As a result, you will need to upgrade to Firebase 2.x.x and add a new .indexOn rule to your Security and Firebase Rules to get the best performance. You can view the updated rules here and read our docs for more information about indexing your data.

Including GeoFire in your project Android/Java

In order to use GeoFire in your project, you need to add the Firebase Android SDK. After that you can include GeoFire with one of the choices below.

Note that after version 1.1.1 the artifact com.firebase:geofire is no longer updated and has been replaced by the separate Android and Java artifacts as described below.

Gradle

Add a dependency for GeoFire to your gradle.build file.

For Android applications:

dependencies {

  compile 'com.firebase:geofire-android:2.1.3' 
}

For non-Android Java applications:

dependencies {

  compile 'com.firebase:geofire-java:2.1.3' 
}
 

Maven

GeoFire also works with Maven.

For Android applications:

<dependency>
<groupId>com.firebase</groupId>
<artifactId>geofire-android</artifactId>
<version>2.1.3</version> </dependency>

For non-Android Java applications:

<dependency>
<groupId>com.firebase</groupId>
<artifactId>geofire-java</artifactId>
<version>2.1.3</version> </dependency>

Jar-File

You can also download the jar file from the latest release on the releases page.

Getting Started with Firebase

GeoFire requires the Firebase database in order to store location data. You can sign up here for a free account.

Quickstart

This is a quickstart on how to use GeoFire's core features. There is also a full API reference available online.

GeoFire

A GeoFire object is used to read and write geo location data to your Firebase database and to create queries. To create a new GeoFire instance you need to attach it to a Firebase database reference.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("path/to/geofire");
 GeoFire geoFire = new GeoFire(ref);

Note that you can point your reference to anywhere in your Firebase database, but don't forget to setup security rules for GeoFire.

Setting location data

In GeoFire you can set and query locations by string keys. To set a location for a key simply call the setLocation method. The method is passed a key as a string and the location as a GeoLocation object containing the location's latitude and longitude:

geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));

To check if a write was successfully saved on the server, you can add a GeoFire.CompletionListener to the setLocation call:

geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973), new GeoFire.CompletionListener() {

  @Override
  public void onComplete(String key, FirebaseError error) {

if (error != null) {

 System.err.println("There was an error saving the location to GeoFire: " + error);

}
 else {

 System.out.println("Location saved on server successfully!");

}

  
}
 
}
);

To remove a location and delete it from the database simply pass the location's key to removeLocation:

geoFire.removeLocation("firebase-hq");

Retrieving a location

Retrieving a location for a single key in GeoFire happens with callbacks:

geoFire.getLocation("firebase-hq", new LocationCallback() {

  @Override
  public void onLocationResult(String key, GeoLocation location) {

if (location != null) {

 System.out.println(String.format("The location for key %s is [%f,%f]", key, location.latitude, location.longitude));

}
 else {

 System.out.println(String.format("There is no location for key %s in GeoFire", key));

}

  
}

@Override
  public void onCancelled(DatabaseError databaseError) {

System.err.println("There was an error getting the GeoFire location: " + databaseError);

  
}
 
}
);

Geo Queries

GeoFire allows you to query all keys within a geographic area using GeoQuery objects. As the locations for keys change, the query is updated in realtime and fires events letting you know if any relevant keys have moved. GeoQuery parameters can be updated later to change the size and center of the queried area.

// creates a new query around [37.7832, -122.4056] with a radius of 0.6 kilometers GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(37.7832, -122.4056), 0.6);

Receiving events for geo queries

There are five kinds of events that can occur with a geo query:

  1. Key Entered: The location of a key now matches the query criteria.
  2. Key Exited: The location of a key no longer matches the query criteria.
  3. Key Moved: The location of a key changed but the location still matches the query criteria.
  4. Query Ready: All current data has been loaded from the server and all initial events have been fired.
  5. Query Error: There was an error while performing this query, e.g. a violation of security rules.

Key entered events will be fired for all keys initially matching the query as well as any time afterwards that a key enters the query. Key moved and key exited events are guaranteed to be preceded by a key entered event.

Sometimes you want to know when the data for all the initial keys has been loaded from the server and the corresponding events for those keys have been fired. For example, you may want to hide a loading animation after your data has fully loaded. This is what the "ready" event is used for.

Note that locations might change while initially loading the data and key moved and key exited events might therefore still occur before the ready event is fired.

When the query criteria is updated, the existing locations are re-queried and the ready event is fired again once all events for the updated query have been fired. This includes key exited events for keys that no longer match the query.

To listen for events you must add a GeoQueryEventListener to the GeoQuery:

geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {

  @Override
  public void onKeyEntered(String key, GeoLocation location) {

System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));

  
}

@Override
  public void onKeyExited(String key) {

System.out.println(String.format("Key %s is no longer in the search area", key));

  
}

@Override
  public void onKeyMoved(String key, GeoLocation location) {

System.out.println(String.format("Key %s moved within the search area to [%f,%f]", key, location.latitude, location.longitude));

  
}

@Override
  public void onGeoQueryReady() {

System.out.println("All initial data has been loaded and events have been fired!");

  
}

@Override
  public void onGeoQueryError(DatabaseError error) {

System.err.println("There was an error with this query: " + error);

  
}
 
}
);

You can call either removeGeoQueryEventListener to remove a single event listener or removeAllListeners to remove all event listeners for a GeoQuery.

Updating the query criteria

The GeoQuery search area can be changed with setCenter and setRadius. Key exited and key entered events will be fired for keys moving in and out of the old and new search area, respectively. No key moved events will be fired; however, key moved events might occur independently.

Updating the search area can be helpful in cases such as when you need to update the query to the new visible map area after a user scrolls.

Deployment

  • In your local environment set $BINTRAY_USER and $BINTRAY_KEY to your Bintray.com username and API key.
  • Checkout and update the master branch.
  • Run ./release.sh to build and deploy.
  • On bintray.com, publish the draft artifacts.
  • Update firebase-versions with the changelog from this version
  • tweet the release

API Reference

A full API reference is available here.

Contributing

If you want to contribute to GeoFire for Java, clone the repository and just start making pull requests.

git clone https://github.com/firebase/geofire-java.git

Resources

ActivitySwitcher is based on the Activity view operation management library, you can achieve any jump between Activity, close any Activity and end applications and other functions.

Simplify Android permission with in-build message Dialogs.

Features:

  • InBuild Rational Dialog with custom message
  • InBuild SettingScreen Dialog with custom Message.
  • Callback for permission state:
    • Permission granted
    • Permission removed
    • Permission access removed.

This is the SampleApp for Android Project Structure.

A simple audio player for Android that you can plugin to your apps quickly get audio playback working.

Android screen or view recorder.

A easy and simple library to apply Gaussian blur on images.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes