CL位置距位置的距离
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有两个坏习惯。
-1067024384
如何?经度值是度。这意味着根据经度的定义,其有效范围被限制在 -90.0 ~ +90.0 之间。您的经度值超出范围。这意味着其中之一。您打印的值错误或实际值错误。模拟器可以打印错误的值。或者你用错误的方法打印了值。你必须尝试:
如果此后仍继续出现不好的结果,
另外,我建议检查所有这样的中间值。
You have two bad habits.
-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:
If bad result continues after that,
And also, I recommend checking all of intermediate values like this.
尝试 @"%f" 并且不要那样投射。
在CLLocation.h中
Try @"%f" and don't cast it that way.
In CLLocation.h