Demand:
From the address filled in manually, analyze the provincial and urban information
thinking
Use Baidu map api
just/inverse Geocoding
PS: although Baidu map provides Location search , but it needs to be transferred to the Administrative Region
According to the positive geocoding service interface, the address is converted into latitude and longitude, and then the reverse geocoding service interface is called according to latitude and longitude to get the structured administrative region information
Code
define("BAIDU_MAP_AK", "Your Baidu map 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; } } //Return result array ( 'country' => 'China', 'country_code' => 0, 'country_code_iso' => 'CHN', 'country_code_iso2' => 'CN', 'province' => 'Fujian Province', 'city' => 'Xiamen City', 'city_level' => 2, 'district' => 'XX area', 'town' => '', 'adcode' => '350206', 'street' => 'XX road', 'street_number' => '26', 'direction' => 'nearby', 'distance' => '33', );