ios distanceFromLocation - 计算两点之间的距离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3905896/
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
distanceFromLocation - Calculate distance between two points
提问by Stephen
Just a quick question on Core Location, I'm trying to calculate the distance between two points, code is below:
只是一个关于核心位置的快速问题,我正在尝试计算两点之间的距离,代码如下:
-(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.
}
I'm getting the following error on the last two lines:
我在最后两行收到以下错误:
error: cannot convert to a pointer type
错误:无法转换为指针类型
I've been searching Google, but I cannot find anything.
我一直在谷歌搜索,但我找不到任何东西。
回答by deanWombourne
Try this instead:
试试这个:
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
The method you're trying to use is a method on a CLLocationobject :)
您尝试使用的方法是CLLocation对象上的方法:)
回答by Atulkumar V. Jain
The distance is calculated between 2 CLLocations and not between to coordinates.
距离是在 2 个 CLLocations 之间计算的,而不是在坐标之间计算的。
You need to use these coordinates to get the CLLocations for the respective coordinates using the following line of code
您需要使用这些坐标来使用以下代码行获取相应坐标的 CLLocations
CLLocation *newLocation = [[CLLocation alloc] initWithCoordinate: newCoordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
Similarly for the other coordinate and then you can calculate the distance between these two locations using the following line of code
同样对于另一个坐标,然后您可以使用以下代码行计算这两个位置之间的距离
CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation] / 1000;
Hope this will help you.
希望这会帮助你。
Update:Swift 3.0
更新:Swift 3.0
let distanceKiloMeters = (newLocation.distance(from: oldLocation))/1000
回答by Atulkumar V. Jain
In Swift
在斯威夫特
Let's create a method function that calculates distance between two locations:
让我们创建一个方法函数来计算两个位置之间的距离:
func distanceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{
var distanceMeters = source.distanceFromLocation(destination)
var distanceKM = distanceMeters / 1000
let roundedTwoDigit = distanceKM.roundedTwoDigit
return roundedTwoDigit
}
If you want only two digits:
如果你只想要两位数:
extension Double{
var roundedTwoDigit:Double{
return Double(round(100*self)/100)
}
}
回答by Max Alexander
If you're going to do with 2 CLLocationCoordinate2D values then you can use this.
如果您打算使用 2 个 CLLocationCoordinate2D 值,那么您可以使用它。
This is Swift 2.1 on Xcode 7.1
这是 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)
}
}
回答by Yogesh Tarsariya
#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
回答by Pawel
The problem here is that you're calling an object method:
这里的问题是您正在调用对象方法:
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;
of class CLLocation.
CLLocation 类的。
CLLocationCoordinate2D is in fact a structure, consisting of two doubles:
CLLocationCoordinate2D 实际上是一个结构体,由两个双精度组成:
typedef struct
{
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;
Proper way of doing this is to get a CLLocation
object and call distanceFromLocation
on it. Like this:
正确的做法是获取一个CLLocation
对象并调用distanceFromLocation
它。像这样:
CLLocation* newLocation;
CLLocation* oldLocation;
CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
Of course you first need to initialize both of those values (from CLLocationManager
, for instance).
当然,您首先需要初始化这两个值(CLLocationManager
例如from )。
回答by zerodog
Taken from the excellent libary CoreLocation utitlities:
取自优秀的库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
}