简体   繁体   English

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

[英]Need help debugging CLLocation code to find distance between two points

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 GPSViewController.h

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

GPSViewController.m 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"? 在上面的示例代码中,如何将变量“distance”从“CLLocationDistance”类型转换为“double”类型?

I'm simply trying to calculate the distance between two points using two different CLLocation objects, and print it to the console. 我只是试图使用两个不同的CLLocation对象计算两个点之间的距离,并将其打印到控制台。 lat1, lon1, lat2, lon2 are doubles. lat1,lon1,lat2,lon2是双精度。

Can anyone see where I am going wrong? 任何人都可以看到我错在哪里?

CLLocationDistance is a double, so you don't need to try to convert to double or whatsoever. CLLocationDistance是双精度型,因此您无需尝试转换为双精度型或任何其他值。

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM