根据百度地图的api自动获取地理位置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hj960511/article/details/84063387

功能:根据百度地图的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;
}

猜你喜欢

转载自blog.csdn.net/hj960511/article/details/84063387