Skip to content

Instantly share code, notes, and snippets.

@imvision
Created August 5, 2014 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imvision/152632a3e3db40580504 to your computer and use it in GitHub Desktop.
Save imvision/152632a3e3db40580504 to your computer and use it in GitHub Desktop.
Calculate distance between coordinates
* 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