以腾讯地图为例,首先要封装一个调取api接口的函数
/**
* $location 要获取地址的经纬度 格式‘39.071510,117.190091’即纬度与经度用逗号隔开
* $key 腾讯地图开发者秘钥
* get_poi 是否返回周边地址列表 1是 0否
*/
public function getCitys($lat,$keys) {
$url = 'https://apis.map.qq.com/ws/geocoder/v1/?location='.$lat.'&key='.$keys.'&get_poi=0';
$info = file_get_contents($url); //请求API接口,请求方式为GET
$info = json_decode($info, true); //返回数据转码为JSON
return $info;
}
通过这个函数可以取得接口返回的城市信息,再对返回信息进行解析
//根据坐标获取省市区
$s=$this->getCitys($send['lat'].','.$send['lon'],Config::get('app.mapqq.keys'));
$send['province'] = $s['result']['ad_info']['province']?$s['result']['ad_info']['province']:'';
$send['city'] = $s['result']['ad_info']['city']?$s['result']['ad_info']['city']:'';
$send['district'] = $s['result']['ad_info']['district']?$s['result']['ad_info']['district']:'';
这样城市信息即可获得