需要帮助调试 CLLocation 代码以查找两点之间的距离

发布于 2024-10-01 08:09:21 字数 954 浏览 2 评论 0原文

我正在尝试在基于视图的应用程序中部署以下代码,但是当我尝试运行它时出现以下错误:

GPSViewController.h

CLLocationManager *lm;
CLLocation *firstLocation;
CLLocation *secondLocation;
CLLocationDistance *distance;
NSString *distanceString;

GPSViewController.m

firstLocation = [[[CLLocation alloc] initWithLatitude:lat1 longitude:lon1] autorelease];
secondLocation = [[[CLLocation alloc] initWithLatitude:lat2 longitude:lon2] autorelease];
distance = [secondLocation distanceFromLocation:firstLocation]; //CLLocation may not respond to distanceFromLocation

double dist = distance / 1000;  //invalid operands to binary

distanceString = [[NSString alloc] stringWithFormat: @"%f", dist];
NSLog(@"the distance between the two points is: %@", distanceString); 

在上面的示例代码中,如何转换变量“距离”从类型“CLLocationDistance”到类型“double”?

我只是尝试使用两个不同的 CLLocation 对象计算两点之间的距离,并将其打印到控制台。 lat1、lon1、lat2、lon2 是双精度数。

谁能看到我哪里出错了?

I am trying to deploy the following code in my View-Based app, but I am getting the following errors when I try to run it:

GPSViewController.h

CLLocationManager *lm;
CLLocation *firstLocation;
CLLocation *secondLocation;
CLLocationDistance *distance;
NSString *distanceString;

GPSViewController.m

firstLocation = [[[CLLocation alloc] initWithLatitude:lat1 longitude:lon1] autorelease];
secondLocation = [[[CLLocation alloc] initWithLatitude:lat2 longitude:lon2] autorelease];
distance = [secondLocation distanceFromLocation:firstLocation]; //CLLocation may not respond to distanceFromLocation

double dist = distance / 1000;  //invalid operands to binary

distanceString = [[NSString alloc] stringWithFormat: @"%f", dist];
NSLog(@"the distance between the two points is: %@", distanceString); 

In my example code above, how do I convert the variable "distance" from a type "CLLocationDistance" to a type "double"?

I'm simply trying to calculate the distance between two points using two different CLLocation objects, and print it to the console. lat1, lon1, lat2, lon2 are doubles.

Can anyone see where I am going wrong?

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

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

发布评论

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

评论(1

献世佛 2024-10-08 08:09:22

CLLocationDistance 是双精度型,因此您无需尝试转换为双精度型或其他任何类型。

你可以像这样计算你的数据,它会起作用:

CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation];  // distance is expressed in meters

CLLocationDistance kilometers = distance / 1000.0;
NSString *distanceString = [[NSString alloc] initWithFormat: @"%f", kilometers];

CLLocationDistance is a double, so you don't need to try to convert to double or whatsoever.

You can compute your data like this, it will work:

CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation];  // distance is expressed in meters

CLLocationDistance kilometers = distance / 1000.0;
NSString *distanceString = [[NSString alloc] initWithFormat: @"%f", kilometers];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文