Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance between two lat,lon points

Is it ok to compare distances in a classic way (distance between 2 points: d = sqrt(pow(lat2-lat1, 2) + pow(lon2-lon1, 2)) ) using the latitude and longitude returned from google apis without any transformation to meters or sth? I need it just for a comparison to find the closest point from a series of points to a reference point. For example:

Lets say we have two (lat,lon) points: (40.2535425,22.88245345) and (40.2565795,22.8884539) and we want to find witch is closest to (40.2335425,22.83245345). Is it ok to apply the above code to find the distances? Or we need to find the distance, lets say in meters (using the haversine formula or whatever), first for each point from the reference point and then compare the values ?

I ask this question because I don't know what exactly are the values returned by google apis as lat, lon! I mean the are not deg-min-sec are they ?

Thanks...

like image 542
Vassilis Avatar asked Apr 04 '11 12:04

Vassilis


People also ask

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

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.

What is the distance between 2 latitude?

-The distance between two successive latitudes is approximately 111 km and the distance of each degree at the equator is 110.567 km.

How do you find the distance between two lines of longitude?

As a general rule, the formula for the ​distance between meridians​ is therefore ​(2πR)(cos L)​, where L is latitude and R is the radius of the Earth.

How many miles are between lines of longitude?

But a degree of longitude, one degree east or west, is a different distance at different points on the globe. At the equator, a degree of longitude is the same as a degree of latitude, about 69 miles (111 km). But it decreases as you move closer to the north or south pole.


2 Answers

No, because lines of longitude converge towards the poles. If your points are relatively close together, you can approximate the distance thus:

d = sqrt(pow(lat2-lat1, 2) + cos(lat1)*pow(lon2-lon1, 2))

If you need greater accuracy over large distances, there are several fancy formulae for computing great-circle distances, but I find it simpler to convert to 3D coordinates on a unit circle then do a simple pythagorean distance, followed by 2 sin-1(d/2) to convert back to an angle (though I can understand that some might find not find this simpler, :-).

like image 130
Marcelo Cantos Avatar answered Oct 08 '22 09:10

Marcelo Cantos


You can also use the computeDistanceBetween() from the new Geometry Library which i think returns the distance in meters

like image 36
Argiropoulos Stavros Avatar answered Oct 08 '22 08:10

Argiropoulos Stavros