gpt4 book ai didi

swift - 如何使用谷歌地图 API 快速查找用户当前位置附近的标记

转载 作者:行者123 更新时间:2023-11-30 12:58:45 24 4
gpt4 key购买 nike

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

最佳答案

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

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

Returns the distance (in meters) from the receiver’s location to the specified location.

这是苹果的 Link

使用

Swift 3.x code

让我们假设有一个变量 CLLocationuserLocation其中存在用户位置的纬度和经度。所以要计算距离userLocation的距离给您markerLocation并且有一个名为 marker 的对象的GMSMarker

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 。所以你可以相应地编写你的逻辑。

关于swift - 如何使用谷歌地图 API 快速查找用户当前位置附近的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40099799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com