【发布时间】:2021-05-26 19:28:48
【问题描述】:
我在 Firebase Firestore 中为每个用户的纬度和经度定位坐标。我只想通过用户在特定半径内的坐标来查询用户。比如50公里。如果我只知道每个用户的纬度和经度,如何做到这一点?
【问题讨论】:
-
如果我的回答对您有所帮助,我们将不胜感激投票或检查它是否正确。
标签: ios swift google-cloud-firestore location
我在 Firebase Firestore 中为每个用户的纬度和经度定位坐标。我只想通过用户在特定半径内的坐标来查询用户。比如50公里。如果我只知道每个用户的纬度和经度,如何做到这一点?
【问题讨论】:
标签: ios swift google-cloud-firestore location
let userLocation = CLLocation(latitude: 6.0, longitude: 6.0)
let coordinate1 = CLLocation(latitude: 6.1, longitude: 6.1)
let coordinate2 = CLLocation(latitude: 5.0, longitude: 3.0)
let coordinate3 = CLLocation(latitude: 7.0, longitude: 3.0)
let coordinatesArray = [coordinate1, coordinate2, coordinate3]
for coordinate in coordinatesArray {
if userLocation.distance(from: coordinate) < 50000 {
print("Inside 50 km")
}
}
注意distance(from:) 方法以米为单位,这就是为什么我要检查它是否在50000m(50km) 内。应该为坐标1执行打印语句,因为它在15.6 km之外。
【讨论】: