/** * [batchLocation description] * @param [type] $locations [description] 经纬度 * @return [type] [description] */public function getBatchLocation( $locations ){$api = config('setting.amap.api.geocode_regeo');//url:https://restapi.amap.com/v3/geocode/regeo$data = ['output' => 'json','location' => $locations,'radius' => 1000,'extensions' => 'all','batch' => 'true','key' => $this->key,//key值];$resp = $this->Curl->get($api, $data);//curl请求return $resp;} 实例调用:$postions = $longitude. ',' .$latitude; //$longitude经度 $latitude纬度$locations = json_decode($this->getBatchLocation(implode('|', $postions)), true);if( $locations['status'] == 1 ) { $locations = $locations['regeocodes']; $address = $locations['formatted_address'];//详细地址 $street = $locations['addressComponent']['township'];//街道 } PHP根据两地经纬度计算距离
/** * 求两个已知经纬度之间的距离,单位为米 * * @param lng1 $ ,lng2 经度 * @param lat1 $ ,lat2 纬度 * @return float 距离,单位米 * @author www.Alixixi.com */ public static function getDistance($lng1, $lat1, $lng2, $lat2) { // 将角度转为狐度 $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度 $radLat2 = deg2rad($lat2); $radLng1 = deg2rad($lng1); $radLng2 = deg2rad($lng2); $a = $radLat1 - $radLat2; $b = $radLng1 - $radLng2; $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000; return $s; }示例: $latitude = '30.624762'; $longitude='104.13498'; $latitude1 = '30.676048'; $longitude1='104.144013'; $distance = round($this->getDistance($latitude, $longitude, $latitude1, $longitude1) / 1000, 1);/***方法二*/ function distance($lat1, $lon1, $lat2,$lon2,$radius = 6378.137) { $rad = floatval(M_PI / 180.0); $lat1 = floatval($lat1) * $rad; $lon1 = floatval($lon1) * $rad; $lat2 = floatval($lat2) * $rad; $lon2 = floatval($lon2) * $rad; $theta = $lon2 - $lon1; $dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta)); if ($dist < 0 ) { $dist += M_PI; } return $dist = $dist * $radius; }示例: $latitude = '30.624762'; $longitude='104.13498'; $latitude1 = '30.676048'; $longitude1='104.144013'; $distance = round($this->distance($latitude, $longitude, $latitude1, $longitude1) , 1);