通过经纬度获取特定半径的用户

时间:2021-02-24 13:06:58

标签: ios swift google-cloud-firestore location

我在 Firebase Firestore 中为每个用户提供了具有纬度和经度的位置坐标。我只想通过用户在特定半径内的坐标来查询用户。例如,50 公里。如果我只知道每个用户的纬度和经度,如何做到这一点?

1 个答案:

答案 0 :(得分: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")
        }
    }

请注意,distance(from:) 方法以米为单位,这就是我检查它是否在 50000m(50km) 内的原因。应该为坐标 1 执行打印语句,因为它离 15.6 km 远。

相关问题