Last active
November 21, 2017 06:46
-
-
Save Sivamanim/17b028766ef6903f3478bbe68eaf591a to your computer and use it in GitHub Desktop.
distance between two latitude and longitude using php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* PHP */ | |
//Get latitude and longitude from geo data | |
$latitudeFrom = '11.016844'; | |
$longitudeFrom = '76.955832'; | |
$latitudeTo = '11.232103'; | |
$longitudeTo = '77.106716'; | |
$unit = 'z'; | |
$theta = $longitudeFrom - $longitudeTo; | |
$dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) + cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta)); | |
$dist = acos($dist); | |
$dist = rad2deg($dist); | |
$miles = $dist * 60 * 1.1515; | |
$unit = strtoupper($unit); | |
if ($unit == "K") { | |
echo ($miles * 1.609344).' km'; | |
} else if ($unit == "N") { | |
echo ($miles * 0.8684).' nm'; | |
} else { | |
echo $miles.' mi'; | |
} | |
// MySQL Query | |
Example: | |
(i) select 1.609344* 3956 * 2 * ASIN(SQRT( POWER(SIN((11.016844 - 11.232103) * pi()/180 / 2), 2) +COS(11.016844 * pi()/180) * COS(11.232103 * pi()/180) * POWER(SIN((76.955832 - 77.106716) * pi()/180 / 2), 2) )) AS distance | |
(ii) Select 1.609344 * 3956 * 2 * ASIN(SQRT( POWER(SIN((" . $latitude . " - sa.shop_latitude) * pi()/180 / 2), 2) +COS(" . $latitude . " * pi()/180) * COS(sa.shop_latitude * pi()/180) * POWER(SIN((" . $longitude . " - sa.shop_longitude) * pi()/180 / 2), 2) )) AS distance | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment