Find nearby businesses from your database, based on user coordinates

Source: Internet
Author: User
Tags pow

Based on user coordinates, locate the nearest merchant to the database:
Using the TP framework
<?php
/**
* Four points of a square that calculates the distance around a certain latitude and longitude
* Earth radius with an average radius of 6371km
* @param LNG float longitude
* @param lat float latitude
* @param distance Float the radius of the circle in which the point is located, the circle is tangent to this square, and the default value is 0.5 km
* Latitude and longitude coordinates of four points of the @return array square
*/
function Getaroundcoordinate ($lng, $lat, $distance = 0.5) {

$DLNG = 2 * ASIN (SIN ($distance/(2 * 6371))/cos (Deg2rad ($lat)));
$DLNG = Rad2deg ($DLNG);

$dlat = $distance/6371;
$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 ' and $LNG-$DLNG),
' Right-bottom ' =>array (' lat ' = $lat-$dlat, ' lng ' = + $lng + $dlng)
);
}
Virtual Data Results


//Call the above function
Public function Actiongetnearshop () {
$scope = 5;//5000 m
$lng = Trim (I (' lng/s '));//Longitude
$lat = Trim (I (' lat/s '));//Latitude
$fourpoint = getaroundcoordinate ($lng, $lat, $scope);
//query This range of shops from the database
$where = "longitude>". $fourpoint [' Right-bottom ' [' lat ']. "and longitude<". $fourpoint [' left-top '] [lat ']. " and latitude< ". $fourpoint [' left-top '] [' LNG ']." and latitude> ". $fourpoint [' Right-bottom '] [' LNG '];
$storeCoordinate = M (' Store ')->where ($where)->column (' Store_id,store_name,longitude,latitude ', ' store_ Id ');
foreach ($storeCoordinate as $k = = $v) {
if (sqrt ((($lat-$v [' latitude ']) * 111) + (POW (((($LNG-$v [' Longitude ') * 111), 2))) <= 5) {
$storeCoordinate [$v [' store_id ']][' distance '] = sqrt ((((($lat-$v [' latitude ']) + (POW (([[]) + ((([')] + ((($LNG-$v " Longitude ']) (111000), 2));
}
}
Echo Json_encode ($storeCoordinate);
}

/**
*According to Baidu coordinates, access to surrounding businesses
*@param $lng longitude
* @ param $ $lat latitude
* @ param $scope range km
Span style= "color: #75715e; font-family: "Arial" "> * @ return array $fourpoint
* */
< Span style= "COLOR: #75715e" >public function index () {
$LNG =trim (I (' lng/s ', 114.067345)); Longitude
$lat =trim (i (' lat/s ', 22.632611));//latitude
$scope = Trim (I (' scope/d ', 5));//5-kilometer
//paging required, statistics on total number of eligible conditions
$ count= db::query ("Select COUNT (store_id) as num from Tp_store WHERE SQRT (Pow ((($lng-longitude) * 111), 2) + (POW (($lat- Latitude) (* 111), 2))) <= $scope ");
$Page =new Page ($count [0][' num '],10);
//Isolate the required data and coordinates from the merchant
$store =db::query ("Select Store_id,store_name,round ((POW (($lng-longitude) * 111), 2 ) + (Pow (((($lat-latitude) * 111), 2))), 2) as distance from ' __prefix__store ' WHERE SQRT ((Pow ((($lng-longitude) * 111), 2) + (POW (((($lat-latitude) * 111), 2))) <= $scope LIMIT {$Page->firstrow},{$Page->listrows} ");
return $store;
}
These two methods are to query the whole table, or consume too much memory. Like a better way, want to share!!

Find nearby businesses from your database, based on user coordinates

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.