Question

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?

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top