2010-11-10 43 views
0

我试图部署在我看来,基于应用程序下面的代码之间的距离,但我收到以下错误,当我尝试运行它:需要帮助调试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”一类“双”?

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

任何人都可以看到我要去哪里错了吗?

+0

距离不应该是一个指针,只是一个普通的CLLocationDistance(这仅仅是一个typedef定义双) – 2010-11-10 07:08:54

回答

2

CLLocationDistance是一个double,所以你不需要尝试转换为double或anysoever。

你可以计算你的数据是这样,它会工作:

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

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

romrom的答案是正确的,但他的代码具有误导性。由CLLocationDistance表示的距离以米为单位,因此将其除以1000将为您提供公里,而不是毫米。 – Ryan 2010-11-10 08:24:48

+0

对不起,我正在修复它 – romrom 2010-11-10 09:41:57

+0

非常感谢您的及时回复。我按照你的建议,我得到以下错误信息:\t CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation]; //不兼容的类型在初始化 – syedfa 2010-11-11 04:56:00