通过纬度和经度吸引特定半径的用户 [英] Get users from certain radius by latitude and longitude
本文介绍了通过纬度和经度吸引特定半径的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已在Firebase Firestore中为每个用户指定了经度和纬度的位置坐标.我只想按用户在特定半径内的坐标查询.例如50公里.如果我只知道每个用户的纬度和经度,怎么办?
I've locations coordinates in Firebase Firestore for each user with latitude and longitude. I want to query only users by their coordinates in certain radius. For example, 50km. How this can be done if I know only latitude and longitude for each user?
推荐答案
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
.
Note that distance(from:)
method is measured in meters, thats why I am checking if it is inside 50000m(50km)
. Print statement should be executed for coordinate1 because it is 15.6 km
away.
这篇关于通过纬度和经度吸引特定半径的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文