需要帮助调试 CLLocation 代码以查找两点之间的距离
我正在尝试在基于视图的应用程序中部署以下代码,但是当我尝试运行它时出现以下错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CLLocationDistance 是双精度型,因此您无需尝试转换为双精度型或其他任何类型。
你可以像这样计算你的数据,它会起作用:
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: