iOS - 当搜索半径太宽时,GeoFire 异常抛出 'Precision must be less than 23!'

标签 ios swift firebase mkmapview geofire

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

当用户决定搜索时,首先我获取用户的位置,然后让用户选择一英里半径,然后让 map View 缩放到该半径:

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)

然后我想获取 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 或 30 英里的 MapView 缩放半径时,如何运行 GeoFire 查询来匹配该半径?

最佳答案

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

正确的方法是double the number of miles you want to span then multiple that by 1609.344 (1 英里等于 1609.344 米)并将结果设置为 CLLocationDistance 以及您在 MKCooperativeRegionMakeWithDistance() 内的位置(下面的步骤 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 https://stackoverflow.com/a/5025970/4833705
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
}

关于iOS - 当搜索半径太宽时,GeoFire 异常抛出 'Precision must be less than 23!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131526/

相关文章:

ios - 从 Any 向下转换为 Specific 类型

javascript - 如何检查数据是否写入firebase数据库?

ios - 无法将 MacOS 框架与 CMake 链接

ios - 将 .xib 文件连接到自定义类

ios - App Store Connect 消息 : Your account will soon need to be migrated to federated auth

ios - “永远不会被执行”

Java 使用 REST API 向 Firebase 实时数据库发送数据问题

ios - 在一个 tableView 上同时获取 2 个 URLRequests

ios - Xcode 8.0 beta 找不到这样的模块 Alamofire Swift 3.0

methods - Swift 的 Superinit 问题