0

I'm sort of a newbie--please don't hate.

The method compiles, but I'm not sure how to actually retrieve the float value (i.e. the distance between the two points) that the method returns (or should return rather).

-(float)findDistanceBetween:(Coordinate *)a and:(Coordinate *)b
{
//distance formula: 
//sqrt( (x2 - x1)^2 + (y2 - y1)^2 )

float resultDistance; 

resultDistance = sqrt( pow((b.latitude - a.latitude), 2) + pow((b.longitude - a.longitude), 2));

return resultDistance;  
}

//Somewhere else...

float theDistanceBetween; 

//Below is incorrect:

theDistanceBetween = [findDistanceBetween: location1 and: location2]; 

Thanks

3
  • After replacing the line: theDistanceBetween = [findDistanceBetween: location1 and: location2] With: theDistanceBetween = [self findDistanceBetween: location1 and: location2]; I get a "self undeclared" error
    – Alec
    May 10, 2011 at 21:11
  • 1
    Once you've solved the immediate problem, you may want to re-examine how you calculate the distance. The equation you use is for distance on a plane, not on a sphere. It could work for short distances except that latitude and longitude are not distance measurements. They are usually expressed as degrees. While degrees latitude are fixed (roughly 69 miles per degree), degrees longitude are not (0 at the poles, 69 miles at the equator). Google "great circle distance" to see how to determine the distance between any two points on the globe.
    – Ferruccio
    May 10, 2011 at 22:00
  • Ferruccio, thank you for your insight. I chose to avoid accounting for the curvature of the earth for the sake of simplicity. You are entirely correct in your criticism
    – Alec
    May 10, 2011 at 23:24

5 Answers 5

2

So, if your error is that self is undeclared, that means that you are trying to send -findDistanceBetween:and: from outside the context of the class that declares it.

When you do something like [obj method], that demands a few things:

  1. That obj is an instance of an Objective-C class.
  2. That the class of obj implements -method.

So, if the receiver of your message is self, that means that:

  1. You need to be within the context of a class's implementation for the self to be implicitly declared.

  2. The class you're in needs to be the same one as implements -findDistanceBetween:and:.

Methods are not just a shiny replacement for functions that can be called in any context. They can be called on objects that implement them (technically not "called" in Smalltalk-like languages such as Objective-C, but that's for another time).

I suspect that you have larger design issues as well. What kind of object is -findDistanceBetween:and: meant to be sent to? If it is a utility method in a class that does something bigger, then it should be a class method (+findDistanceBetween:and:), since it does not need to know about any specific instance. If, however, it is a method on Coordinate, then it'd be better expressed as -findDistanceTo:, which would take a coordinate parameter. And then the implementation of that would compare the provided coordinate parameter with self.

1

findDistanceBetween:and: is an instance method; it's something a particular instance of your class can do.

So you'd call it like:

theDistanceBetween = [self findDistanceBetween: location1 and: location2]; 

Which means "send the message 'findDistanceBetween: location1 and: location2' to the object 'self', and store the result to theDistanceBetween". self just means the current object; it's an object sending a message to itself.

2
  • Thanks for the thorough response Tommy. Adding "self" didn't work however; I'm getting a "self undeclared" warning. Any ideas on how to proceed?
    – Alec
    May 10, 2011 at 20:59
  • To be honest, any answer I give would probably just be a less comprehensive rephrasing of Jonathan Sterling's above. I recommend you focus on his advice, though I'm glad I could help initially.
    – Tommy
    May 10, 2011 at 22:41
0

Is findDistanceBetween:and: defined in an @implementation block? Your code should look something like

   // in the .h file

   @interface MyClass : NSObject

   // declaration of the method
   - (float)findDistanceInBetween:(Coordinate *)a and:(Coordinate *)b;
   @end

   // in the .m file

   @implementation MyClass

   // definition of the method
   - (float)findDistanceBetween:(Coordinate *)a and:(Coordinate *)b {
       return sqrt(powf(a.x - b.x, 2.f) + powf(a.y - b.y, 2.f));
   }
   @end

   // then somewhere else that needs to calculate the difference:
   // (assume coord_a and coord_b already exist)

   // create an instance of MyClass
   MyClass *myInstance = [[MyClass alloc] init];

   // send the `findDistanceBetween:and:` message to the instance
   float distance = [myInstance findDistanceBetween:coord_a and:coord_b];

   // when you're done with the instance, you need to clean up
   [myInstance release];

It may make more sense to put these types of methods on the Coordinate class itself so you can just do something like:

   Coordinate *coord_a = <get the coordinate from somewhere>;
   Coordinate *coord_b = <get the coordinate from somewhere>;

   float distance = [coord_a distanceFrom:coord_b];
   float angle = [coord_a angleTo:coord_b];

Don't worry about anyone hating, we were all new at this once. :)

1
  • I'm glad it was helpful. Welcome to Stack Overflow... I'd be grateful if you'd accept the answer by clicking on the checkmark. :) May 10, 2011 at 22:00
0

Try to add -(float)findDistanceBetween:(Coordinate *)a and:(Coordinate *)b; to your .h file

0
0

As mentioned by Ferruccio above latitude and longitude are in degrees and you method should be returning some form of distance. Below are some methods that will calculate distance as well as bearing between 2 CLLocationCoordinate2Ds.

- (double)degreeToRadian:(double)degree{
return (degree * (M_PI/180.0));
}

- (double)radianToDegree:(double)radian{
return (radian *(180.0/M_PI));
}

-(double) distanceFromCordinate:(CLLocationCoordinate2D)fromCoord to:(CLLocationCoordinate2D)toCoord {
double radiusOfEarth = 6371.0;
double fromLongitude, fromLatitude, toLongitude, toLatitude;
double _deltaLongitude, _deltaLatitude;

double a, c;

fromLongitude = [self degreeToRadian:fromCoord.longitude];
fromLatitude = [self degreeToRadian:fromCoord.latitude];
toLongitude = [self degreeToRadian:toCoord.longitude];
toLatitude = [self degreeToRadian:toCoord.latitude];

_deltaLongitude = toLongitude - fromLongitude;
_deltaLatitude = toLatitude - fromLatitude;

a = (sin(_deltaLatitude/2) * sin(_deltaLatitude/2)) + ( cos(fromLatitude) * cos(toLatitude) * (sin(_deltaLongitude/2) * sin(_deltaLongitude/2)) );
c = 2 * atan2( sqrt(a), sqrt(1-a));
return (radiusOfEarth * c); 
}

- (double)bearingFromCordinate:(CLLocationCoordinate2D)fromCoord to:(CLLocationCoordinate2D)toCoord{
double fromLatitude, toLatitude;    

double _deltaLongitude;

double x, y;

double bearing;

fromLatitude = [self degreeToRadian:fromCoord.latitude];
toLatitude = [self degreeToRadian:toCoord.latitude];

deltaLongitude = [self degreeToRadian:(toCoord.longitude - fromCoord.longitude)];

y = sin(deltaLongitude) * cos(toLatitude);
x = (cos(fromLatitude) * sin(toLatitude)) - (sin(fromLatitude) * cos(toLatitude) * cos(deltaLongitude));
bearing = atan2(y,x);
return fmod(([self radianToDegree:bearing] + 360.0), 360.0) ;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.