mysql query to calculate distance between latitude longitude code example

Example: sql select km from longitude lalitude distance php

// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
$maxLon = $lon + rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
$minLon = $lon - rad2deg(asin($rad/$R) / cos(deg2rad($lat)));

$sql = "Select Id, Postcode, Lat, Lon
        From MyTable
        Where Lat Between :minLat And :maxLat
          And Lon Between :minLon And :maxLon";


$r = $db->prepare("SELECT * FROM location WHERE laltutide Between :minlat AND :maxlat
                   AND longetude between :minlon AND :maxlon");

$r->execute();

$r = $r->fetchAll(PDO::FETCH_OBJ);

Tags:

Php Example