Created
August 5, 2014 06:57
-
-
Save imvision/152632a3e3db40580504 to your computer and use it in GitHub Desktop.
Calculate distance between coordinates
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
* Calculate distance between two users | |
*/ | |
function distanceBetween( $user_num_1, $user_num_2 ) { | |
$sql = "SELECT user_num,lat,lng,locationDiscovery FROM user_profile WHERE user_num in ('$user_num_1','$user_num_2')"; | |
$qry = $this->db->query( $sql ); | |
$users = array(); | |
foreach( $qry->result() as $row ) { | |
$users[$row->user_num] = $row; | |
} | |
if( !array_key_exists($user_num_1, $users) OR !array_key_exists($user_num_2, $users) ) { | |
return -1; | |
} | |
// if users location discovery is off, or lat/long are not available, we can not calculate distance | |
if( $users[$user_num_2]->locationDiscovery == 0 ) { | |
return -1; | |
} | |
$lat1 = $users[$user_num_1]->lat; | |
$lng1 = $users[$user_num_1]->lng; | |
$lat2 = $users[$user_num_2]->lat; | |
$lng2 = $users[$user_num_2]->lng; | |
return $this->haversineGreatCircleDistance( $lat1, $lng1, $lat2, $lng2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment