Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate the distance between two points of latitude and longitude? [duplicate]

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

like image 613
Ankit Vyas Avatar asked Apr 16 '10 10:04

Ankit Vyas


People also ask

How do you find the distance between two points using latitude and longitude?

from math import cos, asin, sqrt, pi def distance(lat1, lon1, lat2, lon2): p = pi/180 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2 return 12742 * asin(sqrt(a)) #2*R*asin... And for the sake of completeness: Haversine on Wikipedia.

How do you find the distance between two latitudes?

If you treat the Earth as a sphere with a circumference of 25,000 miles, then one degree of latitude is 25,000/360 = 69.44 miles. A minute is thus 69.44/60 = 1.157 miles, and a second is 1.15/60 = 0.0193 miles, or about 101 feet.

How do I calculate distance using latitude and longitude on Google Maps?

Select Measure distance. To create a path to measure, click anywhere on the map. To add another point, click anywhere on the map. At the bottom, you can find the total distance in miles (mi) and kilometers (km).


2 Answers

CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
NSLog(@"Distance i meters: %f", [location1 distanceFromLocation:location2]);
[location1 release];
[location2 release];

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

#import <CoreLocation/CoreLocation.h>
like image 87
Mads Mobæk Avatar answered Sep 20 '22 02:09

Mads Mobæk


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.

like image 26
Sparky Avatar answered Sep 18 '22 02:09

Sparky