【问题标题】:How to find markers near user current location in swift using google Map API如何使用谷歌地图API快速查找用户当前位置附近的标记
【发布时间】:2016-10-18 04:53:58
【问题描述】:

我有许多标记坐标存储在数组中。我希望能够在地图上找到用户附近的标记。例如,用户附近有 3 个标记。我知道谷歌提供了一个距离矩阵 api 来计算距离(距用户 2 分钟)。我发现的唯一解决方案是在 javascript 中。如果有人可以指导我朝着正确的方向前进,那将非常有帮助。提前谢谢你。

【问题讨论】:

    标签: swift google-maps-api-3


    【解决方案1】:

    您需要使用distance(from location: CLLocation)CLLocation 类。它在CLLocation 类中

    open func distance(from location: CLLocation) -> CLLocationDistance
    

    返回从接收器位置到指定位置的距离(以米为单位)。

    这是苹果的Link

    用法

    Swift 3.x 代码

    让我们假设有一个名为userLocation 的变量CLLocation,其中包含用户位置的纬度和经度。因此,要计算从userLocation 到您的markerLocation 的距离,并且有一个名为markerGMSMarker 类的对象

    let markerLocation = CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude)
    let metres = userLocation.distance(from: markerLocation)
    print(metres) //will be in metres
    

    在这里您将获得您的marker 位置和您的userLocation 之间的距离。所以你可以相应地编写你的逻辑。

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 2011-03-30
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      相关资源
      最近更新 更多