在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:firebase/geofire-java开源软件地址:https://github.com/firebase/geofire-java开源编程语言:Java 96.1%开源软件介绍:GeoFire for Java — Realtime location queries with FirebaseNote This library is only for server side Java development. If you want to use GeoFire
in your Android application, see GeoFire is an open-source library for 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 Realtime Database 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. GeoFire clients are also available for other languages: Integrating GeoFire with your dataGeoFire 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 UsageAssume 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 Upgrading GeoFireUpgrading from GeoFire 1.x to 2.xGeoFire 2.x is based on the new 3.x release of Firebase. Upgrading from GeoFire 1.0.x to 1.1.xWith 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 Including GeoFire in your Java projectIn order to use GeoFire in your project, you need to add the Firebase Admin SDK. After that you can include GeoFire with one of the choices below. GradleAdd a dependency for GeoFire to your dependencies {
implementation 'com.firebase:geofire-java:3.0.0'
}
MavenAdd a dependency for GeoFire to your <dependency>
<groupId>com.firebase</groupId>
<artifactId>geofire-java</artifactId>
<version>3.0.0</version>
</dependency> UsageGeoFireA 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 dataIn GeoFire you can set and query locations by string keys. To set a location for
a key simply call the 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.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 geoFire.removeLocation("firebase-hq"); Retrieving a locationRetrieving 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 QueriesGeoFire allows you to query all keys within a geographic area using // 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 queriesKey EventsThere are five kinds of "key" events that can occur with a geo query:
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 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 Data EventsIf you are storing model data and geo data in the same database location, you may
want access to the These "data event" listeners have all of the same events as the key listeners with one additional event type:
Adding a data event listener is similar to adding a key event listener: geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
@Override
public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
// ...
}
@Override
public void onDataExited(DataSnapshot dataSnapshot) {
// ...
}
@Override
public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {
// ...
}
@Override
public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {
// ...
}
@Override
public void onGeoQueryReady() {
// ...
}
@Override
public void onGeoQueryError(DatabaseError error) {
// ...
}
}); Updating the query criteriaThe 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
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论