CL位置距位置的距离

发布于 2024-10-20 12:36:46 字数 833 浏览 2 评论 0原文

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

Lat: 0 Long: -1067024384

注释的位置是:

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

但是,如果您查看谷歌地图,您会发现这些距离有多近,但根据CL位置。我使用以下代码来确定它们之间的距离。

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。我相信这是不正确的,因此我的代码一定有问题。

如果有的话请指点一下!

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

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 *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.

If there is please can you point it out!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2024-10-27 12:36:46

你有两个坏习惯。

  1. 在需要硬件审查状态的情况下,您不应该依赖模拟器。特别是当您想要正确的测试时。
  2. 您处理类型的方式不正确。所以你无法正确检查值。经度-1067024384如何?经度值是度。这意味着根据经度的定义,其有效范围被限制在 -90.0 ~ +90.0 之间。

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

在具有真实硬件审查器的真实设备上进行测试。

如果此后仍继续出现不好的结果,

检查全部您的应用程序代码。
特别是对于打印、处理数值。检查您在 > 中使用了正确的类型和铸件每种情况。因为你可能习惯在某个地方进行了有bug的操作。

另外,我建议检查所有这样的中间值。

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!

You have two bad habits.

  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,

Review ALL of 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!
淡淡離愁欲言轉身 2024-10-27 12:36:46

尝试 @"%f" 并且不要那样投射。

CLLocation.h

typedef double CLLocationDistance;

Try @"%f" and don't cast it that way.

In CLLocation.h

typedef double CLLocationDistance;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文