Created
October 17, 2016 16:39
-
-
Save fluffyemily/8e4ee527cb51bae3d94aa07dd89ddcdd to your computer and use it in GitHub Desktop.
Querying Geofire with Swift
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
let geofireRef = FIRDatabase.database().reference() | |
guard let geofire = GeoFire(firebaseRef: geofireRef.child("venues/")) else { return } | |
geofire.getLocationForKey(placeKey) { (location, error) in | |
if let error = error { | |
print("An error occurred getting the location for \(placeKey): \(error.localizedDescription)") | |
} else if let location = location { | |
print("Location for \(placeKey) is [\(location.coordinate.latitude), \(location.coordinate.longitude)]") | |
} else { | |
print("GeoFire does not contain a location for \(placeKey)") | |
} | |
} | |
let center = CLLocation(latitude: 19.9263136, longitude: -155.8868328) | |
if let circleQuery = geofire.query(at: center, withRadius: 1.0) { | |
_ = circleQuery.observe(.keyEntered) { (key, location) in | |
print("Key '\(key)' entered the search area and is at location '\(location)'") | |
} | |
circleQuery.observeReady{ | |
print("All initial data has been loaded and events have been fired for circle query!") | |
} | |
} | |
// Query location by region | |
let span = MKCoordinateSpanMake(0.005, 0.005) | |
let region = MKCoordinateRegionMake(center.coordinate, span) | |
if let regionQuery = geofire.query(with: region) { | |
_ = regionQuery.observe(.keyMoved) { (key, location) in | |
print("Key '\(key)' moved within search area and is at location '\(location)'") | |
} | |
regionQuery.observeReady{ | |
print("All initial data has been loaded and events have been fired for region query!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment