SoFunction
Updated on 2025-03-09

Search for nearby people PHP implementation code

The examples in this article share with you the specific code of PHP to search nearby people for your reference. The specific content is as follows

Implementation ideas:

First of all, we should think this way: Since we know the latitude and longitude of the user's current location and the range we are going to search for, can we calculate a range? That is, based on a central point and radius, the maximum and minimum values ​​of the latitude and longitude that meets the conditions are calculated.

Specific implementation:

So at this point, friends who want to think independently can stop reading.
We mentioned an implementation principle of this function above, and then we will explain the specific implementation steps.
Let's first declare a function to calculate the range of latitude and longitude:

/**
  * Calculate the range based on latitude and longitude and radius
  * @param string $lat
  * @param String $lng Longitude
  * @param float $radius
  * @return Array range array
  */
private function calcScope($lat, $lng, $radius) {
  $degree = (24901*1609)/360.0;
  $dpmLat = 1/$degree;

  $radiusLat = $dpmLat*$radius;
  $minLat = $lat - $radiusLat;    // Minimum latitude  $maxLat = $lat + $radiusLat;    // Maximum latitude
  $mpdLng = $degree*cos($lat * (PI/180));
  $dpmLng = 1 / $mpdLng;
  $radiusLng = $dpmLng*$radius;
  $minLng = $lng - $radiusLng;   // Minimum longitude  $maxLng = $lng + $radiusLng;   // Maximum longitude
  /** Return range array */
  $scope = array(
    'minLat'  => $minLat,
    'maxLat'  => $maxLat,
    'minLng'  => $minLng,
    'maxLng'  => $maxLng
    );
  return $scope;
}

The returned array contains the maximum and minimum latitude and longitude that meets the criteria within the range of $radius.
Now that we have obtained the range, we can start looking for all records that meet the criteria in this latitude and longitude range from the database:

/**
  * Search all power stations within this range based on latitude and longitude and radius
  * @param String $lat
  * @param String $lng Longitude
  * @param float $radius
  * @return Array Calculated results
  */
public function searchByLatAndLng($lat, $lng, $radius) {
  $scope = $this->calcScope($lat, $lng, $radius);   // Call the range calculation function to obtain the maximum and minimum latitude and longitude  /** Query the detailed address of the power station with latitude and longitude in the range of $radius */
  $sql = 'SELECT `Field` FROM `Table Name` WHERE `Latitude` < '.$scope['maxLat'].' and `Latitude` &gt; '.$scope['minLat'].' and `Longitude` &lt; '.$scope['maxLng'].' and `Longitude` &gt; '.$scope['minLng'];

  $stmt = self::$db-&gt;query($sql);
  $res = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);    // Get the query result and return  return $res;
}

Extensions:

Until now, we have known how to calculate the people nearby, but in actual needs, we often need to calculate the actual distance between each person and the current center point.
Next, let's look at another method:

/**
  * Get the distance between two latitudes and longitudes
  * @param string $lat1
  * @param String $lng1
  * @param String $lat2
  * @param String $lng2
  * @return float Returns the distance between two points
  */
public function calcDistance($lat1, $lng1, $lat2, $lng2) {
  /** Convert data type to double */
  $lat1 = doubleval($lat1);
  $lng1 = doubleval($lng1);
  $lat2 = doubleval($lat2);
  $lng2 = doubleval($lng2);
  /** The following algorithm is produced by Google and is consistent with the results of most latitude and longitude calculation tools */
  $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);
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.