PHP queries nearby locations based on a given latitude and longitude point? Rational use of algorithms-php Tutorial

Source: Internet
Author: User
Tags asin
PHP queries nearby locations based on a given latitude and longitude point? Rational use of algorithms
The implementation principle first calculates the four points of the rectangle around the point, and then uses the longitude and latitude to directly match the records in the database. // Obtain the surrounding coordinate public function returnSquarePoint ($ lng, $ lat, $ distance = 0.5) {$ earthRadius = 6378138; $ dlng = 2 * asin (sin ($ distance/(2 * $ earthRadius)/cos (deg 2rad ($ lat); $ dlng = rad2deg ($ dlng ); $ dlat = $ distance/$ earthRadius; $ dlat = rad2deg ($ dlat ); return array ('left-top' => array ('lat' => $ lat + $ dlat, 'lng '=> $ lng-$ dlng ), 'Right-top' => array ('lat' => $ lat + $ dlat, 'lng '=> $ lng + $ dlng ), 'Left-bottom' => array ('lat' => $ lat-$ dlat, 'lng '=> $ lng-$ dlng ), 'Right-bottom' => array ('lat' => $ lat-$ dlat, 'lng '=> $ lng + $ dlng ));} // calculate the straight line distance between two coordinates. public function getDistance ($ lat1, $ lng1, $ lat2, $ lng2) {$ earthRadius = 6378138; // converts the approximate Earth radius meter to Radian $ lat1 = ($ lat1 * pi ()/180; $ lng1 = ($ lng1 * pi ()/180; $ lat2 = ($ lat2 * pi ()/180; $ lng2 = ($ lng2 * pi ()/180; // use the semi-positive vector formula to calculate $ calclong133 = $ lng2-$ lng1; $ calcLatitude = $ lat2-$ lat1; $ stepOne = pow (sin ($ calcLatitude/2), 2) + cos ($ lat1) * cos ($ lat2) * pow (sin ($ calcLongitude/2 ), 2); $ stepTwo = 2 * asin (min (1, sqrt ($ stepOne); $ calculatedDistance = $ earthRadius * $ stepTwo; return round ($ calculatedDistance );}

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.