PHP, Mysql-query nearby locations based on a given latitude and longitude point

Source: Internet
Author: User
: This article mainly introduces PHP, Mysql-query nearby locations based on a given latitude and longitude point. if you are interested in PHP tutorials, refer to it.
Define (EARTH_RADIUS, 6371); // Earth radius, the average radius is 6371 km/***, and the four points of the square with a certain distance around a certain latitude and longitude are calculated ** @ param lng float longitude * @ param lat float latitude * @ param distance float radius of the circle where the point is located, this circle is tangent to this square. the default value is 0.5 km * @ return array coordinate of the longitude and latitude of the four points of the square */function returnSquarePoint ($ lng, $ lat, $ distance = 0.5) {$ dlng = 2 * asin (sin ($ distance/(2 * EARTH_RADIUS)/cos (deg 2rad ($ lat); $ dlng = rad2deg ($ dlng ); $ dlat = $ distance/EARTH_RADIUS; $ dlat = rad 2deg ($ 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);} // use this function to calculate the result and bring it to the SQL query. $ Squares = returnSquarePoint ($ lng, $ lat); $ info_ SQL = "select id, locateinfo, lat, lng from 'lbs _ info' where lat <> 0 and lat >{$ squares ['right-bottom '] ['lat']} and lat <{$ squares ['left -top '] ['lat']} and lng> {$ squares ['left-top'] ['lng']} and lng <{$ squares ['right-bottom '] ['lng']} ";

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.