Finding the nearby people/vehicle through latitude and longitude

Now that we know the latitude and longitude of the user’s current location and the range we are going to search, i.e. a center point and radius, the maximum and minimum values of longitude and latitude.

First declare a function to calculate the range of latitude and longitude

 /**
  * Calculate the range based on the latitude, longitude and radius
  * @param string $latitude
  * @param String $longitude
  * @param float $radius 
  * @return Array 
  */
 private function calcScope($latitude, $longitude, $radius) {
   $degree = (24901 * 1609) / 360.0;
   $dpmLatitude = 1 / $degree;
   $radiusLatitude = $dpmLatitude * $radius;
   $minLatitude = $latitude - $radiusLatitude;    // min latitude
   $maxLatitude = $latitude + $radiusLatitude;    // max latitude
   
   $mpdLongitude = $degree * cos($latitude * (PI / 180));
   $dpmLongitude = 1 / $mpdLongitude;
   $radiusLongitude = $dpmLongitude * $radius;
   $minLongitude = $longitude - $radiusLongitude;   // min longitude
   $maxLongitude = $longitude + $radiusLongitude;   // max longitude
   
   $scope = array(
     'minLatitude'  => $minLatitude,
     'maxLatitude'  => $maxLatitude,
     'minLongitude'  => $minLongitude,
     'maxLongitude'  => $maxLongitude
     );
   return $scope;
 } 

The returned array contains the maximum and minimum latitude and longitude that meet the conditions

Then we can find all the records that meet the conditions from the database

 /**
  * search those latitude, longitude are within range
  * @param String $latitude  
  * @param String $longitude  
  * @param float $radius 
  * @return Array     
  */
 public function searchByLatAndLng($latitude, $longitude, $radius) {
   $scope = $this->calcScope($latitude, $longitude, $radius);   // get the min and max for latitude and logitude
 

   $sql = 'SELECT id, type, latitude, longitude FROM last_location WHERE `latitude` < '.$scope['maxLatitude'].' and `latitude` > '.$scope['minLatitude'].' and `longitude` < '.$scope['maxLongitude'].' and `longitude` > '.$scope['minLongitude'];
   $sth = $dbh->query($sql);
   $result = $sth->fetchAll(PDO::FETCH_ASSOC); 
   return $result;
 } 

Now we have the result that are within the range, we could calculate the actual distance.

/**
  * calculate the distance (KM)
  * @param string $lat1 latitude 1
  * @param String $lng1 longitude 1
  * @param String $lat2 latitude 2
  * @param String $lng2 longitude 2
  * @return float  return the distance in km
  */
 public function calcDistance($lat1, $lng1, $lat2, $lng2) {
 

   $lat1 = doubleval($lat1);
   $lng1 = doubleval($lng1);
   $lat2 = doubleval($lat2);
   $lng2 = doubleval($lng2);
   
   /** calculation by google */
   $theta = $lng1 - $lng2;
   $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
   $dist = acos($dist);
   $dist = rad2deg($dist);
   $miles = $dist * 60 * 1.1515;
   return ($miles * 1.609344);
 } 
This entry was posted in Development and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *