关于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.
}
我在最后两行收到以下错误:
错误:无法转换为指针类型
我一直在搜索谷歌,但我找不到任何东西。
答案 0 :(得分:111)
请改为尝试:
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
您尝试使用的方法是CLLocation对象上的方法:)
答案 1 :(得分:22)
距离是在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
答案 2 :(得分: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)
}
}
答案 3 :(得分: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 :(得分:4)
这里的问题是你正在调用一个对象method:
- (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
)。
答案 5 :(得分: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
答案 6 :(得分:2)
取自优秀的图书馆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
}