xcode CLLocation distanceFromLocation
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5198996/
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
CLLocation distanceFromLocation
提问by max_
I am using CLLocation to work out the distance from the current user location, and an annotation. However I just wanted to know if this would be correct. I am currently using iPhone Simulator for this and according to the MKMapView the iPhone Simulator is situated here:
我正在使用 CLLocation 计算与当前用户位置的距离和注释。但是,我只是想知道这是否正确。我目前为此使用 iPhone 模拟器,根据 MKMapView,iPhone 模拟器位于此处:
Lat: 0 Long: -1067024384
The annotation's position is:
注释的位置是:
workingCoordinate.latitude = 40.763856;
workingCoordinate.longitude = -73.973034;
However if you take a look in google maps you will find out how close these distances are, yet so far apart according to CLLocation. I am using the following code to determine the distance between them both.
但是,如果您查看谷歌地图,您会发现这些距离有多近,但根据 CLLocation 的数据却相距甚远。我正在使用以下代码来确定它们之间的距离。
CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance dist = [loc distanceFromLocation:loc2];
int distance = dist
NSLog(@"%i", distance);
The distance being NSLogged is 12769908. I believe that this is incorrect, and therefore there must be a problem with my code.
NSLogged的距离是12769908。我认为这是不正确的,因此我的代码肯定有问题。
If there is please can you point it out!
有的话请指点一下!
回答by Eonil
You have two bad habits.
你有两个坏习惯。
- You should not depend on simulator in situations need hardware censor status. Especially when you want correct test.
- You're handling typesincorrectly. So you can't check values correctly. How is the longitude
-1067024384
? Longitude value is degrees. This means it's valid range is limited -90.0 ~ +90.0 by definition of longitude.
- 在需要硬件状态的情况下,您不应依赖模拟器。特别是当你想要正确的测试时。
- 您正在错误地处理类型。所以你不能正确检查值。经度如何
-1067024384
?经度值为度。这意味着它的有效范围被经度定义限制在 -90.0 ~ +90.0 之间。
Your longitude value is out of range. This means one of these. You printed the value wrongly or the real value was wrong. Simulator can print wrong value. Or you printed the value with wrong method. You have to try:
您的经度值超出范围。这意味着其中之一。您错误地打印了值或实际值是错误的。模拟器可以打印错误的值。或者你用错误的方法打印了这个值。你得试试:
Test on real device which has real hardware censors.
在具有真正硬件器的真实设备上进行测试。
If bad result continues after that,
如果在那之后继续糟糕的结果,
Review ALLof your application code. Especially for printing, handling values. Check you're using correct types and castings in > each situations. Because you may did buggy operation in somewhere habitually.
查看您的所有应用程序代码。特别是用于打印、处理值。检查您在 > 每种情况下都使用了正确的类型和转换。因为你可能习惯性地在某处做过马车操作。
And also, I recommend checking all of intermediate values like this.
而且,我建议像这样检查所有中间值。
CLLocationCoordinate2D annocoord = annotation.coordinate;
CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate;
NSLog(@"ANNO = %f, %f", annocoord.latitude, annocoord.longitude);
NSLog(@"USER = %f, %f", usercoord.latitude, usercoord.longitude);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
NSLog(@"LOC = %f, %f", loc.coordinate.latitude, loc.coordinate.longitude);
NSLog(@"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude);
CLLocationDistance dist = [loc distanceFromLocation:loc2];
NSLog(@"DIST: %f", dist); // Wrong formatting may show wrong value!
回答by bioffe
Try @"%f" and don't cast it that way.
尝试 @"%f" 并且不要那样转换。
In CLLocation.h
在CLLocation.h
typedef double CLLocationDistance;