Measuring the distance between two points Lat / Lng

I am having problems trying to return the closest place to a user through an SQL statement. For verification, I use the exact coordinates, here is what I have:

SQL:

SELECT  `companies`.`customerName` , 
    (3959 * 
        ACOS(
            COS(
                RADIANS(37.367485) * COS(RADIANS(`locations`.`gps_lat`)) * 
                COS(
                    RADIANS(`locations`.`gps_lng`) - RADIANS(-77.399994) + 
                    SIN(RADIANS(37.367485)) * SIN(RADIANS(`locations`.`gps_lat`))
                )
            )
        )
    )
AS  `distance` 
FROM  `locations` 
JOIN  `companies` ON  `locations`.`co_id` 
HAVING  `distance` > 25
ORDER BY distance
LIMIT 0 , 10

Results:

| customerName  | distance           |
| SOME COMPANY  | 1914.41747964854   |


locations table values:

gps_lat   | gps_lng
37.367485 | -77.399994


I used an example from Google , I checked the formula a couple of times, and I can't figure out where I made a mistake. Any help is appreciated.


EDIT:
Since it seems to me that I understand what I am doing:

>was replaced to get a result 1914, obviously more than that 25.

Android -. $_GET MYSQL -.

- SQL Mysql Workbench. , .

+5
2

, , Google , " ". .
. Google, , Haversine.

SQL:

SELECT  `companies`.`customerName` , 
(2 * (3959 * ATAN2(
          SQRT(
            POWER(SIN((RADIANS(37.367485 - `locations`.`gps_lat` ) ) / 2 ), 2 ) +
            COS(RADIANS(`locations`.`gps_lat`)) *
            COS(RADIANS(37.367485 )) *
            POWER(SIN((RADIANS(-77.399994 - `locations`.`gps_lng` ) ) / 2 ), 2 )
          ),
          SQRT(1-(
            POWER(SIN((RADIANS(37.367485 - `locations`.`gps_lat` ) ) / 2 ), 2 ) +
            COS(RADIANS(`locations`.`gps_lat`)) *
            COS(RADIANS(37.367485)) *
            POWER(SIN((RADIANS(-77.399994 - `locations`.`gps_lng` ) ) / 2 ), 2 )
          ))
        )
      ))
AS 'distance'
FROM  `locations` 
JOIN  `companies` ON  `locations`.`co_id` 
HAVING  distance < 25
ORDER BY distance
LIMIT 0 , 10



, , Final PHP:

GET: ?lat=37.367485&lng=-77.399994

$earth_radius_miles = 3959; // Earth radius in miles
$earth_radius_kilometers = 6371; // Earth radius in kilometers
$result_radius = 10000; // Maximum distance in either miles or kilometers

$get_lat = $_GET["lat"];
$get_lng = $_GET["lng"];

$dbSelect = mysql_query("SELECT  `companies`.`customerName`,
(2 * (".$earth_radius_miles." * ATAN2(
          SQRT(
            POWER(SIN((RADIANS(".$get_lat." - `locations`.`gps_lat` ) ) / 2 ), 2 ) +
            COS(RADIANS(`locations`.`gps_lat`)) *
            COS(RADIANS(".$get_lat.")) *
            POWER(SIN((RADIANS(".$get_lng." - `locations`.`gps_lng` ) ) / 2 ), 2 )
          ),
          SQRT(1-(
            POWER(SIN((RADIANS(".$get_lat." - `locations`.`gps_lat` ) ) / 2 ), 2 ) +
            COS(RADIANS(`locations`.`gps_lat`)) *
            COS(RADIANS(".$get_lat.")) *
            POWER(SIN((RADIANS(".$get_lng." - `locations`.`gps_lng` ) ) / 2 ), 2 )
          ))
        )
      ))
AS 'distance'
FROM  `locations` 
JOIN  `companies` ON  `locations`.`co_id` 
HAVING  distance < ".$result_radius."
ORDER BY distance
LIMIT 0 , 10") 
or die(mysql_error());

:

[{"customerName":"SOME COMPANY","distance":"0"}]

, , . , .

+5

, , :

enter image description here

R  =  radius of the earth (6,371 km)
Δlat =  |lat2- lat1|
Δlong = |long2- long1|
+1

All Articles