У меня есть mapView, и пользователи могут выбрать радиус увеличения 1, 10, 20 и 30 миль для поиска ресторанов, где бы они ни находились. У меня есть все латы ресторана, хранящиеся в пути GeoFire.
Когда пользователь решает выполнить поиск, сначала я получаю местоположение пользователя / пользователей, затем я позволяю пользователю выбрать радиус в милю, а затем я получаю mapView для увеличения до этого радиуса:
var selectedRadius = 0.0
let milesToMeters = 1609.344
let doubleDistance = 2 // according to this https://stackoverflow.com/a/5025970/4833705 to get a mapView radius of x miles, the distance needs to be doubled
var circleQuery: GFCircleQuery?
var queryHandle: UInt?
// 1. get the user's current location
guard let currentLocation = locationManager.location else { return }
let lat = currentLocation.coordinate.latitude
let lon = currentLocation.coordinate.longitude
let center = CLLocation(latitude: lat, longitude: lon)
// 2. user picks 10 mile radius
selectedRadius = (10 * doubleDistance) * milesToMeters
// 3. get the region I want to use for zoom in the mapView
let coordinateRegion = MKCoordinateRegionMakeWithDistance(center.coordinate, selectedRadius, selectedRadius)
mapView.setRegion(coordinateRegion, animated: true)
Затем я хочу получить все рестораны в этом радиусе 10 миль от GeoFire, и я запускаю:
// 4. geoFire ref
let geofireRef = Database.database().reference().child("restaurant_locations")
let geoFire = GeoFire(firebaseRef: geofireRef)
// 5. since I had doubled the selectedRadius for the MKCoordinateRegionMakeWithDistance I now cut it in half to get the regular 10 mile radius in meters
let initialRadius = (selectedRadius / doubleDistance)
// 6. this is the radius I want to run the geoFire query on
circleQuery = geoFire?.query(at: center, withRadius: initialRadius)
// 7. observe .keyEntered at that query
queryHandle = circleQuery?.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in ...
На шаге 7 при попытке соблюсти .keyEntered
я получаю исключение:
'Точность должна быть меньше 23!
Затем я попытался установить диапазон, используя запрос GeoFire's Region query(with:)
, но получил точно такое же исключение
let span = MKCoordinateSpanMake(initialRadius, initialRadius)
let region = MKCoordinateRegionMake(center.coordinate, span)
geoFire.query(with: region).observe(.keyEntered, with: { (key: String!, location: CLLocation!) in ...
При выборе радиуса масштабирования MapView в 10, 20 или 30 миль, как я могу выполнить запрос GeoFire для соответствия этому радиусу?