This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jonny Dimond
committed
Jul 13, 2014
0 parents
commit 295c86b
Showing
14 changed files
with
1,050 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Mac OS-X | ||
.DS_STORE | ||
|
||
# idea | ||
.idea | ||
*.iml | ||
*.ipr | ||
*.iws | ||
|
||
# Maven | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.firebase</groupId> | ||
<artifactId>geofire-java</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>geofire-java</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.firebase</groupId> | ||
<artifactId>firebase-client</artifactId> | ||
<version>[1.0.7,)</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.firebase.geofire; | ||
|
||
import com.firebase.client.Firebase; | ||
|
||
public class Example { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Firebase firebase = new Firebase("https://geofire-ios.firebaseio.com/geofire"); | ||
GeoFire geoFire = new GeoFire(firebase); | ||
GeoQuery query = geoFire.queryAtLocation(37.7, -122.4, 10000); | ||
query.addGeoQueryEventListener(new GeoQuery.GeoQueryEventListener() { | ||
@Override | ||
public void onKeyEntered(String key, double latitude, double longitude) { | ||
System.out.println(String.format("%s entered at [%f, %f]", key, latitude, longitude)); | ||
} | ||
|
||
@Override | ||
public void onKeyExited(String key) { | ||
System.out.println(String.format("%s exited", key)); | ||
} | ||
|
||
@Override | ||
public void onKeyMoved(String key, double latitude, double longitude) { | ||
System.out.println(String.format("%s moved to [%f, %f]", key, latitude, longitude)); | ||
} | ||
}); | ||
Thread.sleep(100000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.firebase.geofire; | ||
|
||
import com.firebase.client.DataSnapshot; | ||
import com.firebase.client.Firebase; | ||
import com.firebase.client.FirebaseError; | ||
import com.firebase.client.ValueEventListener; | ||
import com.firebase.geofire.core.GeoHash; | ||
import com.firebase.geofire.util.GeoUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class GeoFire { | ||
|
||
public static interface LocationEventListener { | ||
public void onLocationChanged(String key, double latitude, double longitude); | ||
public void onKeyRemoved(String key); | ||
public void onCancelled(FirebaseError firebaseError); | ||
} | ||
|
||
static double[] getLocationValue(DataSnapshot dataSnapshot) { | ||
try { | ||
//Map data = (Map)dataSnapshot.getValue(Map.class); | ||
//double[] location = (double[])data.get("l"); | ||
double[] location = dataSnapshot.getValue(double[].class); | ||
if (location.length == 2 && GeoUtils.coordinatesValid(location[0], location[1])) { | ||
return location; | ||
} else { | ||
return null; | ||
} | ||
} catch (NullPointerException e) { | ||
return null; | ||
} catch (ClassCastException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private static class LocationValueEventListener implements ValueEventListener { | ||
|
||
private final LocationEventListener eventListener; | ||
private final String key; | ||
|
||
LocationValueEventListener(String key, LocationEventListener eventListener) { | ||
this.key = key; | ||
this.eventListener = eventListener; | ||
} | ||
|
||
@Override | ||
public void onDataChange(DataSnapshot dataSnapshot) { | ||
double[] location = GeoFire.getLocationValue(dataSnapshot); | ||
if (location != null) { | ||
this.eventListener.onLocationChanged(dataSnapshot.getName(), location[0], location[1]); | ||
} else { | ||
this.eventListener.onKeyRemoved(dataSnapshot.getName()); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCancelled(FirebaseError firebaseError) { | ||
this.eventListener.onCancelled(firebaseError); | ||
} | ||
} | ||
|
||
private final Firebase firebase; | ||
private final Map<LocationEventListener, LocationValueEventListener> eventMapping = | ||
new HashMap<LocationEventListener, LocationValueEventListener>(); | ||
|
||
public GeoFire(Firebase firebase) { | ||
this.firebase = firebase; | ||
} | ||
|
||
public Firebase getFirebase() { | ||
return this.firebase; | ||
} | ||
|
||
private Firebase firebaseForKey(String key) { | ||
return this.firebase.child("l").child(key); | ||
} | ||
|
||
public synchronized void setLocation(String key, double latitude, double longitude) { | ||
this.setLocation(key, latitude, longitude, null); | ||
} | ||
|
||
public synchronized void setLocation(String key, double latitude, double longitude, Firebase.CompletionListener listener) { | ||
if (key == null) { | ||
throw new NullPointerException(); | ||
} | ||
Firebase keyFirebase = this.firebaseForKey(key); | ||
GeoHash geoHash = new GeoHash(latitude, longitude); | ||
Map<String, Object> updates = new HashMap<String, Object>(); | ||
updates.put("g", geoHash.getGeoHashString()); | ||
updates.put("l", new double[]{latitude, longitude}); | ||
keyFirebase.setValue(updates, listener); | ||
} | ||
|
||
public synchronized void addLocationEventListener(String key, LocationEventListener listener) { | ||
Firebase keyFirebase = this.firebaseForKey(key); | ||
if (this.eventMapping.containsKey(listener)) { | ||
throw new IllegalArgumentException("Added the same LocationEventListener twice!"); | ||
} | ||
LocationValueEventListener valueListener = new LocationValueEventListener(key, listener); | ||
this.eventMapping.put(listener, valueListener); | ||
keyFirebase.addValueEventListener(valueListener); | ||
} | ||
|
||
public synchronized void removeEventListener(LocationEventListener listener) { | ||
LocationValueEventListener valueListener = this.eventMapping.get(listener); | ||
if (valueListener == null) { | ||
throw new IllegalArgumentException("Did not previously add or already removed listener"); | ||
} | ||
this.firebaseForKey(valueListener.key).removeEventListener(valueListener); | ||
} | ||
|
||
public GeoQuery queryAtLocation(double latitude, double longitude, double radius) { | ||
return new GeoQuery(this, latitude, longitude, radius); | ||
} | ||
} |
Oops, something went wrong.