C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# haversine formula definition for sql

By : davep627
Date : November 21 2020, 07:31 AM
With these it helps The order of lat/lngs don't matter. Think of it this way... the distance from point A to point B is that same as the distance from point B to point A.
In your code example, the 37 is a latitude point and the -122 is a longitude point.
code :
``````SELECT id, ( 3959 * acos( cos( radians(Lat1) ) * cos( radians( Lat2 ) ) * cos( radians(Lng2) - radians(Lng1) ) + sin( radians(Lat1) ) * sin( radians(Lat2)))) AS distance
FROM     markers
HAVING distance < 25
ORDER BY distance
LIMIT 0 , 20;
``````

Share :

## Using the Haversine formula with PostgreSQL and PDO

By : Christy Haragan
Date : March 29 2020, 07:55 AM
wish helps you PostgreSQL does have a radians function:
code :
``````radians(cast(lat as double precision))
``````
``````SELECT id, ( 6371 * acos( cos( radians(51.8391) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(4.6265) ) + sin( radians(51.8391) ) * sin( radians( lat ) ) ) ) AS distance
FROM shops
HAVING ( 6371 * acos( cos( radians(51.8391) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(4.6265) ) + sin( radians(51.8391) ) * sin( radians( lat ) ) ) ) < 25
ORDER BY name asc
``````
``````select id, distance
from (
select id, name, ( 6371 * acos( cos( radians(51.8391) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(4.6265) ) + sin( radians(51.8391) ) * sin( radians( lat ) ) ) ) as distance
from shops
) as dt
where distance < 25.0
order by name asc
``````

## Haversine formula with php

By : Gareth
Date : March 29 2020, 07:55 AM
this will help The formula you used, seems to be the arccosine instead of the haversine formula. The haversine formula is indeed more appropriate to calculate the distance on a sphere, because it is less prone to rounding errors.
code :
``````/**
* Calculates the great-circle distance between two points, with
* the Haversine formula.
* @param float \$latitudeFrom Latitude of start point in [deg decimal]
* @param float \$longitudeFrom Longitude of start point in [deg decimal]
* @param float \$latitudeTo Latitude of target point in [deg decimal]
* @param float \$longitudeTo Longitude of target point in [deg decimal]
* @return float Distance between points in [m] (same as earthRadius)
*/
function haversineGreatCircleDistance(
\$latitudeFrom, \$longitudeFrom, \$latitudeTo, \$longitudeTo, \$earthRadius = 6371000)
{
// convert from degrees to radians

\$latDelta = \$latTo - \$latFrom;
\$lonDelta = \$lonTo - \$lonFrom;

\$angle = 2 * asin(sqrt(pow(sin(\$latDelta / 2), 2) +
cos(\$latFrom) * cos(\$latTo) * pow(sin(\$lonDelta / 2), 2)));
}
``````
``````\$center_lat = 41.8350;
\$center_lng = 12.470;
\$lat = 41.9133741000;
\$lng = 12.5203944000;

// test with your arccosine formula
print(\$distance); // prints 9.662174538188

// test with my haversine formula
\$distance = haversineGreatCircleDistance(\$center_lat, \$center_lng, \$lat, \$lng, 6371);
print(\$distance); // prints 9.6621745381693
``````

## VBA haversine formula

By : Alluri divya teja
Date : March 29 2020, 07:55 AM
help you fix your problem Atan2 is defined back to front in Excel compared to JavaScript i.e. Atan2(x,y) rather than Atan2(y,x).
You need to reverse the order of the two arguments:-
code :
``````c = 2 * Excel.WorksheetFunction.Atan2(Sqr(1 - a), Sqr(a))
``````
``````=haversine(59.3293371,13.4877472,59.3225525,13.4619422)
``````
``````1.65 km
``````

## Is the Haversine Formula or the Vincenty's Formula better for calculating distance?

By : pop toyz
Date : March 29 2020, 07:55 AM
hope this fix your issue Haversine is a simpler computation but it does not provide the high accuracy Vincenty offers.
Vincenty is more accurate but is also more computationally intensive and will therefore perform slower and increase battery usage.

## Haversine formula

By : Gabriel Altemann
Date : March 29 2020, 07:55 AM
around this issue atan(x) is the same as atan2(x,1). atan2(y,x) is the angle of the point (x,y), while atan(y/x) is the angle of the line through the origin and (x,y).
The only inverse trigonometric function present as FPU on x86 CPU/FPUinstruction is FPATAN which implements atan2. Thus also asin(x) is implemented as atan2(x,sqrt(1-x*x)), acos(x) as atan2(sqrt(1-x*x),x).