45

CLLocation我从文档中知道我们可以使用函数找到两点之间的距离, distanceFromLocation:. 但我的问题是我没有 CLLocation 数据类型,我有 CLLocationCoordinate2D 点。那么如何找到两个 CLLocationCoordinate2D 点之间的距离。我看过帖子,但对我没有帮助。

4

8 回答 8

39

您应该使用创建 CLLocation 的对象,

- (id)initWithLatitude:(CLLocationDegrees)latitude
    longitude:(CLLocationDegrees)longitude;

然后,您应该能够使用计算距离

[location1 distanceFromLocation:location2];
于 2012-06-18T05:42:02.287 回答
35

斯威夫特 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-02-23T21:10:13.183 回答
33

如果您可以以米为单位获得点之间的距离,那么

CLLocationCoordinate2D coordinate1 = <some value>
CLLocationCoordinate2D coordinate2 = <some value>
…
MKMapPoint point1 = MKMapPointForCoordinate(coordinate1);
MKMapPoint point2 = MKMapPointForCoordinate(coordinate2);
CLLocationDistance distance = MKMetersBetweenMapPoints(point1, point2);

将返回两点之间的距离。无需CLLocation通过给定创建CLLocationCoordinate2D。此定义自 iOS 4.0 起可用

于 2015-05-25T16:06:44.933 回答
24

Swift4:结合以上想法

extension CLLocationCoordinate2D {
    //distance in meters, as explained in CLLoactionDistance definition
    func distance(from: CLLocationCoordinate2D) -> CLLocationDistance {
        let destination=CLLocation(latitude:from.latitude,longitude:from.longitude)
        return CLLocation(latitude: latitude, longitude: longitude).distance(from: destination)
    }
}
于 2017-12-14T11:21:09.163 回答
6
    let point1 = MKMapPointForCoordinate(myLocation)
    let point2 = MKMapPointForCoordinate(friendLocation)
    let distance = MKMetersBetweenMapPoints(point1, point2)/1000
    let distanceStr = NSString(format: "%.3f", distance)

瓦莱里答案的简单版本。除以 1000 得到 KM,然后转换为字符串。

于 2015-08-03T14:12:38.713 回答
1

Swift 5:计算距离的 CLLocationCoordinate2D 扩展:

extension CLLocationCoordinate2D {
    /// Returns distance from coordianate in meters.
    /// - Parameter from: coordinate which will be used as end point.
    /// - Returns: Returns distance in meters.
    func distance(from: CLLocationCoordinate2D) -> CLLocationDistance {
        let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
        let to = CLLocation(latitude: self.latitude, longitude: self.longitude)
        return from.distance(from: to)
    }
}

用法:

let coordinate = CLLocationCoordinate2D(latitude: 11, longitude: 39)
let from = CLLocationCoordinate2D(latitude: 30, longitude: 43)
let distance = coordinate.distance(from: from)
于 2020-10-19T20:12:19.123 回答
0

CLLocationCoordinate2D+Distance.swift对于 Swift 5.5,基于MapKit

import MapKit

extension CLLocationCoordinate2D {

    /// Returns the distance between two coordinates in meters.
    func distance(to: CLLocationCoordinate2D) -> CLLocationDistance {
        MKMapPoint(self).distance(to: MKMapPoint(to))
    }

}

版本基于CLLocation

import CoreLocation

extension CLLocationCoordinate2D {

    /// Returns the distance between two coordinates in meters.
    func distance(to: CLLocationCoordinate2D) -> CLLocationDistance {
        CLLocation(latitude: latitude, longitude: longitude)
            .distance(from: CLLocation(latitude: to.latitude, longitude: to.longitude))
    }

}

这两个扩展都有性能开销,如果处理大量坐标,这可能是相关的。在这些情况下:

Euclid 包中有一个对 CLLocationCoordinate2D 的扩展,它直接执行计算,请参阅:distance(to:)

于 2021-08-27T19:18:22.830 回答
-2

CLLocationDistance是以米为单位的测量值。

let point1 = CLLocationCoordinate2D(latitude: lat1, longitude: lon1)
let point2 = CLLocationCoordinate2D(latitude: lat2, longitude: lon2)
let distance = point1.distance(to: point2)
于 2019-05-02T01:59:23.280 回答