从手工填写的地址中,解析出省市区信息
使用百度地图api
正/ 逆地理编码
PS: 虽然百度地图提供 地点检索,可是是须要传入行政区信息的
根据 正地理编码服务 接口 将地址转换为经纬度,而后根据经纬度调用 逆地理编码服务 接口,获得结构化的行政区信息php
define("BAIDU_MAP_AK", "你的百度地图AK"); function parse_address($string){ try{ $url = "http://api.map.baidu.com/geocoder/v2/"; $params = [ 'address' => $string, 'output' => "json", 'ak' => BAIDU_MAP_AK, ]; $url .= "?" . http_build_query($params); $json = json_decode(file_get_contents($url), true); if($loc = @$json['result']['location']){ if(!is_null($loc)){ $url = "http://api.map.baidu.com/geocoder/v2/"; $params = [ 'location' => "{$loc['lat']},{$loc['lng']}", 'output' => "json", 'ak' => BAIDU_MAP_AK, ]; $url .= "?" . http_build_query($params); $json = json_decode(file_get_contents($url), true); return @$json['result']['addressComponent']; } } return null; }catch(\Exception $e){ return null; } } //返回结果 array ( 'country' => '中国', 'country_code' => 0, 'country_code_iso' => 'CHN', 'country_code_iso2' => 'CN', 'province' => '福建省', 'city' => '厦门市', 'city_level' => 2, 'district' => 'XX区', 'town' => '', 'adcode' => '350206', 'street' => 'XX路', 'street_number' => '26', 'direction' => '附近', 'distance' => '33', );