我在数组中存储了许多标记坐标。我希望能够在地图上找到用户附近的标记。例如,在用户附近有3个标记。我知道google提供了一个距离矩阵api来计算距离(距离用户2分钟)。我发现的唯一解决方案是使用javascript。如果有人可以指导我正确的方向,那将非常有帮助。先感谢您。
您需要使用distance(from location: CLLocation)
的CLLocation
类。它在CLLocation
课堂上
open func distance(from location: CLLocation) -> CLLocationDistance
返回从接收器位置到指定位置的距离(以米为单位)。
这是苹果的链接
用法
Swift 3.x代码
让我们假设存在一个CLLocation
名为的变量,userLocation
其中用户位置的纬度和经度在那里。所以,从计算距离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
。因此,您可以相应地编写逻辑。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句