/**
* @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;
}
}
每天进步一点,更多精彩内容,欢迎关注公众号“编程社”!