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?
Asked
Active
Viewed 215 times
0
-
Please check this: https://stackoverflow.com/a/66363250/2781088 – Mohit Kumar Feb 25 '21 at 06:10
-
if my answer helped you, it would be appreciated to vote it up or check it as a correct. – stackich Feb 25 '21 at 08:36
1 Answers
0
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")
}
}
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.

stackich
- 3,607
- 3
- 17
- 41
-
Note that OP wants all users within a certain radius, maybe you should adjust your answer for that. – Joakim Danielson Feb 24 '21 at 13:44
-