早睡早起,方能养生!
Sleep early rise early, square can preserve one's health!

WGS84坐标转换为百度坐标

super
2018-12-03 18:10
views 1065

WGS84坐标转换为 百度坐标

 

申请百度地图ak:http://lbsyun.baidu.com/apiconsole/key/create

 

/**
 * 坐标转换 http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition
 * WGS84 -> 百度坐标
 * @param float $lon 经度
 * @param float $lat 纬度
 * @param string $ak 百度地图key
 * @return array|false
 */
function changeGPS($lon, $lat, $ak)
{
    $url  = "http://api.map.baidu.com/geoconv/v1/?coords={$lon},{$lat}&from=1&to=5&ak={$ak}";

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $data = curl_exec($curl);
    curl_close($curl);
    
    $array= json_decode($data, true);
    if ($array['status'] == 0) {
        return ['lon' => $array['result'][0]['x'], 'lat' => $array['result'][0]['y']];
    } else {
        return false;
    }
}

分享
0 条讨论
top