gpt4 book ai didi

iphone - CL位置距位置的距离

转载 作者:行者123 更新时间:2023-12-03 19:06:08 36 4
gpt4 key购买 nike

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

Lat: 0 Long: -1067024384

注释的位置是:

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

但是,如果您查看谷歌地图,您会发现这些距离有多近,但根据 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);

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

如果有请指出!

最佳答案

你有两个坏习惯。

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

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

Test on real device which has real hardware censors.

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

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.

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

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!

关于iphone - CL位置距位置的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5198996/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com