CLLocation distanceFromLocation [英] CLLocation distanceFromLocation

查看:74
本文介绍了CLLocation distanceFromLocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CLLocation来计算当前用户位置和注释的距离。但是我只是想知道这是否正确。我目前正在使用iPhone模拟器,根据MKMapView,iPhone模拟器位于此处:

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:

Lat: 0 Long: -1067024384

注释的位置是:

workingCoordinate.latitude = 40.763856;
workingCoordinate.longitude = -73.973034;

但是,如果你看看谷歌地图,你会发现这些距离有多近,但是根据CLLocation相距甚远。我使用以下代码来确定它们之间的距离。

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 *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);

NSLogged的距离是12769908.我认为这是不正确的,因此一定有问题用我的代码。

The distance being NSLogged is 12769908. I believe that this is incorrect, and therefore there must be a problem with my code.

如果有,请你指出来!

推荐答案

你有两个坏习惯。


  1. 在需要硬件审查状态的情况下,你不应该依赖模拟器。特别是当您想要正确的测试时。

  2. 您正在错误地处理类型。因此您无法正确检查值。经度如何 -1067024384 ?经度值是度。根据经度的定义,这意味着它的有效范围限制为-90.0~ + 90.0。

  1. You should not depend on simulator in situations need hardware censor status. Especially when you want correct test.
  2. You're handling types incorrectly. 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.

您的经度值超出范围。这意味着其中之一。您错误地打印了值或实际值是错误的。模拟器可以打印错误的值。或者您使用错误的方法打印了值。您必须尝试:

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,


查看你的所有应用代码。
特别适用于打印,处理值。在每种情况下检查您是否使用了正确的类型和铸件。因为你可能在惯用的地方做错误操作

而且,我建议检查所有中间值像这样。

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!

这篇关于CLLocation distanceFromLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆