Search nearby people PHP implementation code
- 2021-09-11 19:44:58
- OfStack
This article example for everyone to share PHP to achieve search nearby people specific code, for your reference, the specific content is as follows
Implementation ideas:
First of all, we should think like this: Now that we know the latitude and longitude of the user's current position and the range we are going to search for, can we calculate a range? That is to say, according to one center point and radius, the maximum and minimum values of longitude and latitude that meet the conditions are calculated.
Concrete realization:
So at this point, friends who want to think independently can stop looking down.
Above, we mentioned one implementation principle of this function, and then we will explain one specific implementation step.
Let's first declare a function to calculate the range of latitude and longitude:
/**
* Calculate the range according to longitude, latitude and radius
* @param string $lat Latitude
* @param String $lng Longitude
* @param float $radius 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
/** Returns an array of ranges */
$scope = array(
'minLat' => $minLat,
'maxLat' => $maxLat,
'minLng' => $minLng,
'maxLng' => $maxLng
);
return $scope;
}
The returned array contains the eligible maximum and minimum latitude and longitude in the range of $radius.
Now that we have obtained the range, we can start looking up all the eligible records in this latitude and longitude range from the database:
/**
* Inquire all power stations in this range according to longitude, latitude and radius
* @param String $lat Latitude
* @param String $lng Longitude
* @param float $radius Radius
* @return Array The calculated result
*/
public function searchByLatAndLng($lat, $lng, $radius) {
$scope = $this->calcScope($lat, $lng, $radius); // Call the range calculation function to get the maximum and minimum latitude and longitude
/** Query latitude and longitude in $radius Detailed addresses of power stations within the scope */
$sql = 'SELECT ` Field ` FROM ` Table name ` WHERE `Latitude` < '.$scope['maxLat'].' and `Latitude` > '.$scope['minLat'].' and `Longitude` < '.$scope['maxLng'].' and `Longitude` > '.$scope['minLng'];
$stmt = self::$db->query($sql);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get the query results and return
return $res;
}
Extension:
Until now, we have known how to calculate the nearby people, but in actual needs, we often need to calculate the actual distance between every one person and the current center point.
Next, let's look at another method:
/**
* Get the distance between two latitudes and longitudes
* @param string $lat1 Weft 1
* @param String $lng1 Jing 1
* @param String $lat2 Weft 2
* @param String $lng2 Jing 2
* @return float Returns the distance between two points
*/
public function calcDistance($lat1, $lng1, $lat2, $lng2) {
/** Convert the data type to double */
$lat1 = doubleval($lat1);
$lng1 = doubleval($lng1);
$lat2 = doubleval($lat2);
$lng2 = doubleval($lng2);
/** The following algorithm is Google Come out, and the results of most latitude and longitude calculation tools 1 To */
$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);
}