Requirements: Requirements to be acquired around the business address based on the user's current location, and in accordance with the sort from near to far,
Method a: layer of code implemented
Packaging method:
/ * * * @Desc calculate the distance between two latitude and longitude * @param float $ lat latitude * @param float $ lng longitude * @param $ status true KM, M display; only returns to false M * / function system_getdistance ( LAT1 $ , $ LNG 1 , $ LAT2 , $ lng2 , $ Status = to true , $ SINGLE = to true ) { $ earthRadius = 6.367 million ; // Approximate RADIUS Earth in meters of $ LAT1 = ( of floatval ( $ LAT1 ) * PI ()) / 180 ; $lng1 = (floatval($lng1) * pi()) / 180; $lat2 = (floatval($lat2) * pi()) / 180; $lng2 = (floatval($lng2) * pi()) / 180; $calcLongitude = $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 $ ; IF (! $ SINGLE ) { return round ( $ calculatedDistance ); Exit ; } // convert to more than 1000 meters km IF( $ Status ) { $ m = round ( $ calculatedDistance ) / 1000 ; return $ m ?>. 1 round ( $ m .,. 1) "km": ( $ m . * 1000) "m" ; // return round ( $ m, 2); // this is what I used in the program, that sort more convenient, we selected according to the needs of } the else { return round ( $ calculatedDistance ) "m." ; } }
transfer:
/ * * * @Param the Request Request $ * @return Array * Get store classified press from the sorting * / function getShop (the Request $ Request ) { $ c_id = $ Request -> GET ( 'c_id' ); $ LAT1 = $ Request -> GET ( 'LAT' ); $ LNG 1 = $ Request -> GET ( 'LNG' ); $ lat1U = of floatval ( $ LAT1 ) +0.01 ; $ lat1D = of floatval ( $ LAT1 ) -0.01 ; $ lng1U =of floatval ( $ LNG 1 ) +0.01 ; $ lng1D = of floatval ( $ LNG 1 ) -0.01 ; $ Data = DB ( 'Store' ) -> WHERE ( 'c_id', $ c_id ) -> WHERE ( 'LAT', '<' , $ lat1U ) // Get around the latitude and longitude of differential merchant from 0.01 -> WHERE ( 'LAT', '>', $ lat1D ) -> WHERE ( 'LNG', '<', $ lng1U ) -> WHERE ( 'LNG ','> ', $ lng1D ) -> Find (); $res = json_encode($data); $arr1 = json_decode($res,true); $arr = []; foreach ($arr1 as $k=>$v){ $res = $this->system_getdistance($lat1,$lng1,$v['lat'],$v['lng']); $arr1[$k]['km'] = $res.'km'; $arr[$k] = $res; } asort($arr); $arr2 = []; foreach($arr as $k=>$v){ $arr2[] = $arr1[$k]; } return $arr2; }
Method two: sql layer implementation
SELECT id , year , lng , ROUND ( 6378.138 * 2 * asin ( SQRT ( POW ( SIN ( ( $ years * PI () / 180 - years * PI () / 180 ) / 2 ) , 2 ) + COS ( $ year * PI () / 180) * COS (years *PI() / 180) * POW( SIN( ( $lng * PI() / 180 - lng * PI() / 180 ) / 2 ), 2 ) ) ) * 1000 ) AS distance FROM store ORDER BY distance asc
sql retrieved within five kilometers of
select * from (
SELECT
id,
lat,
lng,
ROUND(
6378.138 * 2 * ASIN(
SQRT( POW( SIN( ( $lat * PI() / 180 - lat * PI() / 180 ) / 2 ), 2 ) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * POW( SIN( ( $lng * PI() / 180 - lng * PI() / 180 ) / 2 ), 2 ) ) ) * 1000 ) AS distance FROM store ORDER BY distance asc
) as a where a.distance<=5000