ios 查找 CLLocationCoordinate2D 点之间的距离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11077425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding distance between CLLocationCoordinate2D points
提问by rakeshNS
I know from documentation we can find distance between two CLLocation
points using the function, distanceFromLocation:
. But my problem is I dont have CLLocation data type with me, I have the CLLocationCoordinate2D points. So how can I find distance between two CLLocationCoordinate2D points. I have seen the post postbut not helpful for me.
我从文档中知道我们可以CLLocation
使用函数distanceFromLocation:
. 但我的问题是我没有 CLLocation 数据类型,我有 CLLocationCoordinate2D 点。那么如何找到两个 CLLocationCoordinate2D 点之间的距离。我看过帖子,但对我没有帮助。
采纳答案by Apurv
You should create an object of CLLocation using,
您应该使用以下方法创建一个 CLLocation 对象,
- (id)initWithLatitude:(CLLocationDegrees)latitude
longitude:(CLLocationDegrees)longitude;
Then, you should be able to calculate the distance using
然后,您应该能够使用计算距离
[location1 distanceFromLocation:location2];
回答by Valerii Lider
If it is ok for you to get distance in meters between points, then
如果你可以得到点之间的距离(以米为单位),那么
CLLocationCoordinate2D coordinate1 = <some value>
CLLocationCoordinate2D coordinate2 = <some value>
…
MKMapPoint point1 = MKMapPointForCoordinate(coordinate1);
MKMapPoint point2 = MKMapPointForCoordinate(coordinate2);
CLLocationDistance distance = MKMetersBetweenMapPoints(point1, point2);
will return the distance between two points. No needs to create CLLocation
by given CLLocationCoordinate2D
. This defines are available since iOS 4.0
将返回两点之间的距离。无需CLLocation
通过 given创建CLLocationCoordinate2D
。此定义自 iOS 4.0 起可用
回答by Aviel Gross
Swift 4:
斯威夫特 4:
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(to)
}
}
回答by Eric Yuan
Swift4:combined above thoughts
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)
}
}
回答by VBaarathi
let point1 = MKMapPointForCoordinate(myLocation)
let point2 = MKMapPointForCoordinate(friendLocation)
let distance = MKMetersBetweenMapPoints(point1, point2)/1000
let distanceStr = NSString(format: "%.3f", distance)
Simple version of Valleri's answer. Divide by 1000 to get KM followed by conversion to string.
Valleri 答案的简单版本。除以 1000 得到 KM,然后转换为字符串。
回答by Alex Chase
CLLocationDistance
is a measurement in meters.
CLLocationDistance
是以米为单位的度量单位。
let point1 = CLLocationCoordinate2D(latitude: lat1, longitude: lon1)
let point2 = CLLocationCoordinate2D(latitude: lat2, longitude: lon2)
let distance = point1.distance(to: point2)