iOS -GeoFire ExceptionThrown 'Precision must be less than 23!' 当搜索半径太宽时

iOS -GeoFire ExceptionThrown 'Precision must be less than 23!' when Search Radius is to Wide

我有一个 mapView,用户可以选择 1 英里、10 英里、20 英里和 30 英里的缩放半径,以便从他们的设备所在的任何地方搜索餐馆。我将餐厅的所有经纬度都保存在 GeoFire 路径中。

当用户决定搜索时,首先获取 user/s 位置,然后让用户选择一英里半径,然后获取 mapView 以缩放到该半径:

var selectedRadius = 0.0
let milesToMeters = 1609.344
let doubleDistance = 2  // according to this  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)

然后我想从 GeoFire 获取 10 英里半径内的所有餐厅,我 运行:

// 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 时出现异常:

'Precision must be less than 23!

然后我尝试使用 GeoFire 的区域查询设置跨度 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 ...

选择10英里,20 mi或30 mi的MapView Zoom Radius时,我如何运行匹配该半径的地理器查询?

我用海里给出的旧答案在地图上显示的距离不正确。

正确的方法是 double the number of miles you want to span then multiple that by 1609.344(1 英里是 1609.344 米)并将结果设置为 CLLocationDistances 以及您在 MKCoordinateRegionMakeWithDistance() 内的位置(下面的第 3 步)

使用 GFRegionQuery(下面的第 6 步)而不是 GFCircleQuery 来使用 geoFire 区域

基本上就是下面第2步中的转换,使用下面第3步中的区域,并将其设置为下面第6步中要使用的区域。

var radiusConversion = 0.0
let milesToMeters = 1609.344
let doubleDistance = 2
var regionQuery: GFRegionQuery?
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 and do the conversion based on this link 
radiusConversion = (10 * doubleDistance) * milesToMeters

// 3. set the region I want to use for zoom in the mapView
let region = MKCoordinateRegionMakeWithDistance(center.coordinate, radiusConversion, radiusConversion)
mapView.setRegion(region, animated: true) // zoom

// 4. geoFire ref
let geoFireRef = Database.database().reference().child("restaurant_locations")
let geoFire = GeoFire(firebaseRef: geoFireRef)

// 6. use GeoFire's query(with: region) and this is the radius I want to run the geoFire query on
regionQuery = geoFire.query(with: region)

// 7. observe.keyEntered at that region query
queryHandle = regionQuery?.observe(.keyEntered, with: {.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in ...

// 8.
regionQuery?.observeReady {
    // do something when the observer from step 7 is finished
}