0

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?

Parcker
  • 219
  • 1
  • 10

1 Answers1

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