Use PHP to search for nearby people features

Source: Internet
Author: User
Tags sin stmt
This time to everyone with PHP to make a search near the function of the people in PHP to make a search near the function of the attention of the matter, the following is the actual case, take a look.

Implementation ideas:

First of all, we should think: Now that we know the latitude and longitude of the user's current position, and know the scope we will search, can we calculate a range? That is, the maximum and minimum values of the latitude and longitude that meet the criteria are calculated based on a center point and a radius.

Specific implementation:

In this case, the small partner who wants to think about it independently can not continue to look down.
We mentioned one of the implementation principles of this function, and then we will explain the specific implementation steps.
Let's declare a function to use as a range for calculating latitude and longitude:

/** * Calculates the range based on latitude and longitude @param string $lat latitude * @param string $lng longitude * @param float $radius radius * @return Array range */priva Te 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;   The maximum longitude  /** returns an array of ranges *  /$scope = Array (    ' minlat ' = =  $minLat,    ' maxlat ' =  $maxLat,    ' minlng '  = $minLng,    ' maxlng ' and  $maxLng    );  return $scope;}

The returned array contains the maximum minimum latitude and longitude that meets the criteria within the $radius range.
Now that we've got the scope, we can start looking up all the records in the database that meet the criteria in this latitude and longitude range:

/** * Search All power plants in this range according to latitude and longitude * @param string $lat  latitude * @param string $lng  longitude * @param float $radius radius * @return Ar Ray     calculates the result */public function searchbylatandlng ($lat, $LNG, $radius) {  $scope = $this->calcscope ($lat, $LNG, $radius);   Call the range calculation function to get the maximum minimum latitude and longitude  /** query the detailed address of the power plant within the $radius range */  $sql = ' SELECT ' field ' from ' Table name ' WHERE ' Latitude ' < '. $sco pe[' Maxlat '. ' and ' Latitude ' > '. $scope [' Minlat ']. ' and ' longitude ' < '. $scope [' maxlng ']. ' and ' Longitude ' > '. $ scope[' minlng '];  $stmt = self:: $db->query ($sql);  $res = $stmt->fetchall (PDO::FETCH_ASSOC);    Gets the query result and returns the return  $res;}

Extended:

Until now, we've learned how to figure out who's nearby, but in actual demand, we often need to figure out the actual distance between each person and the current center point.
Next, let's look at one more method:

/** * Get the distance between two latitude and longitude * @param string $lat 1 Weft one * @param string $LNG 1 by one * @param string $lat 2 weft two * @param string $LNG 2 by two * @ return float returns the distance between two points */public function calcdistance ($lat 1, $lng 1, $lat 2, $lng 2) {  /** the conversion data type is double */  $lat 1 = Doubleval ($lat 1);  $LNG 1 = doubleval ($lng 1);  $lat 2 = Doubleval ($lat 2);  $LNG 2 = Doubleval ($lng 2);  /** the following algorithm is Google out, and most of the latitude and longitude calculation tool results consistent */  $theta = $lng 1-$lng 2;  $dist = sin (Deg2rad ($lat 1)) * Sin (Deg2rad ($lat 2)) + cos (Deg2rad ($lat 1)) * cos (Deg2rad ($lat 2)) * cos (Deg2rad ($theta)); 
   $dist = ACOs ($dist);  $dist = Rad2deg ($dist);  $miles = $dist * 1.1515;  Return ($miles * 1.609344);}

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

What are the steps PHP uses for long redis connections

PHP application container and deployment use of detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.