距离计算 - 计算两点之间的距离

50

关于Core Location,我有一个快速的问题,我正在尝试计算两个点之间的距离,如下所示的代码:

    -(void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation
    {   

    // Configure the new event with information from the location.
        CLLocationCoordinate2D newCoordinate = [newLocation coordinate];
        CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];

        CLLocationDistance kilometers = [newCoordinate distanceFromLocation:oldCoordinate] / 1000; // Error ocurring here.
        CLLocationDistance meters = [newCoordinate distanceFromLocation:oldCoordinate]; // Error ocurring here.
}

我在最后两行遇到以下错误:

错误:无法转换为指针类型

我已经在谷歌上搜索了,但是找不到任何相关信息。

7个回答

114
尝试使用这个替代方案:
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];

你正在尝试使用的方法是CLLocation对象上的一个方法 :)

11
请注意:“此方法通过在两个位置之间绘制一条沿着地球曲度的直线来测量它们之间的距离”,这是苹果公司文档中的说法。因此,这种方法无法提供道路距离。 - Ankit Srivastava
1
如何通过道路找到距离?@deanWombourne - Rahul
好的答案,但在iOS9中,此代码功能比iOS8需要更多时间。您有什么想法如何解决吗? - Code Hunterr
@CodeHunterr 听起来很奇怪 - 它运行得慢多少? - deanWombourne
问题可能出在您对点进行排序的方式上 - 您是在排序之前计算每个点之间的距离,还是在比较两个点时每次都重新计算距离? - deanWombourne
显示剩余4条评论

22

距离是在两个CLLocations之间计算的,而不是在两个坐标之间计算的。

您需要使用以下代码行获取相应坐标的CLLocations,然后计算它们之间的距离:

CLLocation *newLocation = [[CLLocation alloc] initWithCoordinate: newCoordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];

同样地,对于另一个坐标也是如此,然后您可以使用以下代码计算这两个位置之间的距离。

CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation] / 1000;

希望这能帮到你。

更新:Swift 3.0

let distanceKiloMeters = (newLocation.distance(from: oldLocation))/1000

11

使用Swift语言

让我们创建一个方法函数,用于计算两个位置之间的距离:

 func distanceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{

        var distanceMeters = source.distanceFromLocation(destination)
        var distanceKM = distanceMeters / 1000
        let roundedTwoDigit = distanceKM.roundedTwoDigit
        return roundedTwoDigit

    }

如果你只想要两位数:

extension Double{

    var roundedTwoDigit:Double{

        return Double(round(100*self)/100)

        }
    }

1
好的回答。只有一个小问题。在 distanceBetweenTwoLocations 函数内,你应该用 let 替换 var ;-) - Luciano Rodríguez

5

如果你需要处理2个CLLocationCoordinate2D值,那么可以使用以下方法。

这是在Xcode 7.1上的Swift 2.1。

import CoreLocation

extension CLLocationCoordinate2D {

    func distanceInMetersFrom(otherCoord : CLLocationCoordinate2D) -> CLLocationDistance {
        let firstLoc = CLLocation(latitude: self.latitude, longitude: self.longitude)
        let secondLoc = CLLocation(latitude: otherCoord.latitude, longitude: otherCoord.longitude)
        return firstLoc.distanceFromLocation(secondLoc)
    }

}

4
#import <CoreLocation/CoreLocation.h>

CLLocation *locA = [[CLLocation alloc] initWithLatitude:"Value" longitude:"Value"];

CLLocation *locB = [[CLLocation alloc] initWithLatitude:"Value" longitude:"Value"];

CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"distance:-%f",distance);//distance in Miter

需要对这个答案进行解释。 - Burhanuddin Rashid
嗨,Burhanuddin Rashid,这不是安卓开发代码,而是针对“计算两点之间距离”的iOS开发代码。 - Yogesh Tarsariya
无论是iOS、Android还是其他技术领域都不重要。关键是答案应该附带一些代码解释,不要仅仅复制粘贴代码,对于一些解释请有礼貌。 - Burhanuddin Rashid
嗨,Burhanuddin Rashid,这个答案不是复制和粘贴的。它是苹果默认函数来获取距离的。 我的答案很简短,但它完美地工作。 你输入 locA 输入纬度、经度 locB 输入纬度、经度 计算 LocA 到 LocB 的距离。 更多信息链接 谢谢。 - Yogesh Tarsariya

4

这里的问题在于你正在调用一个对象的方法:

方法与Core Location框架相关。
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;

CLLocation类的实例。

CLLocationCoordinate2D实际上是一个结构体,由两个双精度浮点数组成:

typedef struct
{
    CLLocationDegrees latitude;
    CLLocationDegrees longitude;
} CLLocationCoordinate2D;

正确的方法是获取一个CLLocation对象,并在其上调用distanceFromLocation。像这样:

CLLocation* newLocation;
CLLocation* oldLocation;
CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];

当然,你首先需要初始化这两个值(例如从CLLocationManager中),然后再进行操作。

2

摘自优秀的库CoreLocation工具

- (CLLocationDistance) distanceFromCoordinate:(CLLocationCoordinate2D) fromCoord;
{
    double earthRadius = 6371.01; // Earth's radius in Kilometers

    // Get the difference between our two points then convert the difference into radians
    double nDLat = (fromCoord.latitude - self.coordinate.latitude) * kDegreesToRadians;  
    double nDLon = (fromCoord.longitude - self.coordinate.longitude) * kDegreesToRadians; 

    double fromLat =  self.coordinate.latitude * kDegreesToRadians;
    double toLat =  fromCoord.latitude * kDegreesToRadians;

    double nA = pow ( sin(nDLat/2), 2 ) + cos(fromLat) * cos(toLat) * pow ( sin(nDLon/2), 2 );

    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
    double nD = earthRadius * nC;

    return nD * 1000; // Return our calculated distance in meters
}

1
如果有人感兴趣,这是haversine公式 - Dylan Bettermann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接