views:

929

answers:

5

i have latitude and longitude of particular place and i want to calculate the distance so how can i calculate it?

+1  A: 

of two points or what? if yes: http://www.movable-type.co.uk/scripts/latlong.html

PS: IPhone has nothing about it, it is pure math

Andrey
This is wrong, iPhone can calculate the distance between two CLLocations (lat/long)
Mads Mobæk
i meant that is it not about iphone, the formula is general
Andrey
+3  A: 
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
NSLog(@"Distance i meters: %f", [location1 getDistanceFrom:location2]);
[location1 release];
[location2 release];

You also need to add CoreLocation.framework to your project, and add the import statement:

#import <CoreLocation/CoreLocation.h>
Mads Mobæk
Thanks Man!Its Working Nice.Thanks Once again...
Ankit Vyas
+3  A: 

This might not be the most efficient method of doing it, but it will work.

Your two locations specified by latitude and longitude can be considered vectors. Assuming that the coordinates have been converted into cartesion coordinates, calculate the dot product of the two vectors.

Given v1 = (x1, y1, z1) and v2 = (x2, y2, z2), then ...

v1 dot v2 = magnitude(v1) * magnitude(v2) * cos (theta)

Conveniently, the magnitude of v1 and v2 will be the same ... the radius of the earth (R).

x1*x2 + y1*y2 + z1*z2 = R*R*cos(theta)

Solve for theta.

theta = acos ((x1*x2 + y1*y2 + z1*z2) / (R * R));

Now you have angle between the two vectors in radians. The distance betwen the two points when travelling across the surface of earth is thus ...

distance = theta * R.

There is probably an easier way to do this entirely within the context of spherical coordinates, but my math in that area is too fuzzy--hence the conversion to cartesian coordinates.

To convert to cartesian coordinates ...

Let alpha be the latitude, and beta be the longitude.

x = R * cos (alpha) * cos (beta)
y = R * sin (alpha)
z = R * cos (alpha) * sin (beta)

Don't forget that the math function typically deal in radians, and the latitude/longitude deal in degrees.

Sparky
Note that this is an approximation, as the earth is more of an oblate spheroid (it's gone pear shaped, to be more accurate) and its surface isn't flat.
outis
See the `proj.4` website for a discussion of the error in using a spherical model (http://trac.osgeo.org/proj/wiki/GeodesicCalculations) for the earth. Bottom line, they say that if you pick a good value for R, you can get to within 1% with this approach.
mtrw
+1. Presumably this answer could be headed "How to calculate a great circle route" (nautical navigation term). UPVOTE DISCLAIMER: I have zero ability to validate the math on this. I just envy the ability that produced it, and I esteem the clear presentation.
Smandoli
A: 

I've cranked through the math, and can now greatly simplify the solution.

Imagine if we spin the earth so that our first vector is at 0 degrees latitude and 0 degrees longitude. The second vector would be at (alpha2 - alpha1) degrees latitude and (beta2 - beta1) degrees latitude.

Since ...

sin(0) = 0 and cos(0) = 1

our dot product simplies to ...

cos(delta_alpha) * cos(delta_beta) = cos(theta)

The rest of the math remains unchanged.

theta = acos (cos(delta_alpha) * cos(delta_beta))
distance = radius * theta

Hope this helps.

Sparky
A: 

What if a place is located in between two latitudes or two longitudes what should you do?

Pepper