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

问题描述 投票:0回答:1

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

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

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

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

任何人都可以看到我错在哪里?

iphone objective-c cllocation
1个回答
2
投票

CLLocationDistance是double,因此您无需尝试转换为double或any。

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

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

CLLocationDistance kilometers = distance / 1000.0;
NSString *distanceString = [[NSString alloc] initWithFormat: @"%f", kilometers];
© www.soinside.com 2019 - 2024. All rights reserved.