PHP根据经纬度计算两地距离
时间:2015-10-29 阅读:次 QQ群:182913345
<?php //根据经纬度计算两地距离 function getDistance($lat1, $lng1, $lat2, $lng2){ $r = 6378.137; //地球半径 //角度转为弧度 $radLat1 = deg2rad($lat1); $radLat2 = deg2rad($lat2); $radLng1 = deg2rad($lng1); $radLng2 = deg2rad($lng2); //sqrt:平方根,pow(x, n):x的n次方的幂,asin:反正弦,sin:正弦 $s = 2*asin(sqrt(pow(sin(($radLat1 - $radLat2)/2),2)+cos($radLat1)*cos($radLat2)*pow(sin(($radLng1-$radLng2)/2),2)))*$r; return $s; } //根据经纬度查地址 function get_location($location_x,$location_y){ $url = "http://api.map.baidu.com/geocoder/v2/?ak=GHvfRpIKsKLAgGNacrFdG8Uy&location=$location_x,$location_y&output=json&pois=0"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); curl_close($curl); $res = json_decode($data,true); return $res; } //根据地址查经纬度 function get_loca($address){ $url = "http://api.map.baidu.com/geocoder/v2/?address=$address&output=json&ak=GHvfRpIKsKLAgGNacrFdG8Uy"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); curl_close($curl); $res = json_decode($data,true); return $res; } |
上一篇:PHP文件上传类实例讲解
下一篇:php生成随机密码实例