PHP通過地址獲取經緯度【百度地圖API】


/**
 * @param string $address 地址
 * @param string $city  城市名
 * @return array
 */
function getLatLng($address='',$city='')
{
    $result = array();
    $ak = '';//您的百度地圖ak,可以去百度開發者中心去免費申請
    $url ="http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address=".$address."&city=".$city."&ak=".$ak;
    $data = file_get_contents($url);
    $data = str_replace('renderOption&&renderOption(', '', $data);
    $data = str_replace(')', '', $data);
    $data = json_decode($data,true);
    if (!empty($data) && $data['status'] == 0) {
        $result['lat'] = $data['result']['location']['lat'];
        $result['lng'] = $data['result']['location']['lng'];
        return $result;//返回經緯度結果
    }else{
        return null;
    }
 
}

來自微信公眾號:編程社

程序員日常進階寶典,歡迎關注!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM