我从文档中了解到,我们可以使用函数distanceFromLocation:来计算两个CLLocation点之间的距离。但我的问题是我没有CLLocation数据类型,我有CLLocationCoordinate2D点。那么如何找到两个CLLocationCoordinate2D点之间的距离呢?我看过post的帖子,但对我没有帮助。
发布于 2012-06-18 13:42:02
您应该使用以下命令创建CLLocation对象:
- (id)initWithLatitude:(CLLocationDegrees)latitude
longitude:(CLLocationDegrees)longitude;然后,您应该能够使用以下命令计算距离
[location1 distanceFromLocation:location2];发布于 2015-02-24 05:10:13
Swift 5:
extension CLLocation {
/// Get distance between two points
///
/// - Parameters:
/// - from: first point
/// - to: second point
/// - Returns: the distance in meters
class func distance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
return from.distance(from: to)
}
}发布于 2015-05-26 00:06:45
如果您可以以米为单位获取点之间的距离,那么
CLLocationCoordinate2D coordinate1 = <some value>
CLLocationCoordinate2D coordinate2 = <some value>
…
MKMapPoint point1 = MKMapPointForCoordinate(coordinate1);
MKMapPoint point2 = MKMapPointForCoordinate(coordinate2);
CLLocationDistance distance = MKMetersBetweenMapPoints(point1, point2);将返回两点之间的距离。不需要通过给定的CLLocationCoordinate2D创建CLLocation。此定义从iOS 4.0开始可用
https://stackoverflow.com/questions/11077425
复制相似问题