根據百度地圖的api自動獲取地理位置
阿新 • • 發佈:2018-11-14
功能:根據百度地圖的api
版本:php版本
步驟1:獲取地理位置資訊
function get_city($ip){ $url = "http://api.map.baidu.com/location/ip?ak=CKbKvNu2m9SaYuWQgk4zn9wRshaidrt0&ip=$ip&coor=bd09ll"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); if(curl_errno($ch)) { echo 'CURL ERROR Code: '.curl_errno($ch).', reason: '.curl_error($ch);} curl_close($ch); $info = json_decode($output, true); if($info['status'] == "0"){ $lotx = $info['content']['point']['y']; $loty = $info['content']['point']['x']; $citytemp = $info['content']['address_detail']['city']; $keywords = explode("市",$citytemp); $city = $keywords[0]; } else{ $lotx = "34.2597"; $loty = "108.9471"; $city = "無效"; } $area = ['lotx'=>$lotx,'loty'=>$loty,'city'=>$city]; return $area; }