【问题标题】:Get users from certain radius by latitude and longitude [duplicate]通过纬度和经度从特定半径获取用户[重复]
【发布时间】:2021-05-26 19:28:48
【问题描述】:

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

【问题讨论】:

标签: ios swift google-cloud-firestore location


【解决方案1】:
    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之外。

【讨论】:

  • 请注意,OP 希望所有用户都在某个范围内,也许您应该为此调整答案。
  • 是的,我已经编辑了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2014-01-21
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多