distanceFromLocation – 计算两点之间的距离

只是一个关于核心位置的简单问题,我试图计算两点之间的距离,代码如下:

-(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. } 

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

错误:无法转换为指针types

我一直在search谷歌,但我找不到任何东西。

试试这个:

 CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation]; 

你试图使用的方法是一个CLLocation对象的方法:)

距离是在2个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 

在Swift中

我们来创build一个方法函数来计算两个位置之间的距离:

  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) } } 

如果你打算使用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) } } 

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

 - (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 )。

 #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 

采取从优秀的书库CoreLocation utitlities :

 - (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 }