Search code examples
google-maps-api-3geolocation

How to find out corresponding country and region by providing a city to google maps api


I'm creating a weather application and I need to know country and region of a city to provide weather data. When a user searches on a city, the application shows a list of all cities with that name along with country and region.

Is this possible with Google maps API, or is there a better option?


Solution

  • I would recommend using the component parameter of the Google geocoding API. In the example below I am querying for all localities (cities, unincorporated areas, and other named populated places) with the name Moscow. You can see in the XML output that it has both Moscow Russia and Moscow, Idaho. The output also contains the country and 1st level administrative division (e.g., state in the US) (see https://developers.google.com/maps/documentation/geocoding/#ComponentFiltering)

    https://maps.googleapis.com/maps/api/geocode/xml?address=component:locality=Moscow&sensor=false
    
    <GeocodeResponse>
    <status>OK</status>
    <result>
    <type>locality</type>
    <type>political</type>
    <formatted_address>Moscow, Russia</formatted_address>
    <address_component>
    <long_name>Moscow</long_name>
    <short_name>Moscow</short_name>
    <type>locality</type>
    <type>political</type>
    </address_component>
    <address_component>
    <long_name>gorod Moskva</long_name>
    <short_name>g. Moskva</short_name>
    <type>administrative_area_level_2</type>
    <type>political</type>
    </address_component>
    <address_component>
    <long_name>Moscow</long_name>
    <short_name>Moscow</short_name>
    <type>administrative_area_level_1</type>
    <type>political</type>
    </address_component>
    <address_component>
    <long_name>Russia</long_name>
    <short_name>RU</short_name>
    <type>country</type>
    <type>political</type>
    </address_component>
    <geometry>
    <location>
    <lat>55.7558260</lat>
    <lng>37.6173000</lng>
    </location>
    <location_type>APPROXIMATE</location_type>
    <viewport>
    <southwest>
    <lat>55.4899270</lat>
    <lng>37.3193290</lng>
    </southwest>
    <northeast>
    <lat>56.0096570</lat>
    <lng>37.9456610</lng>
    </northeast>
    </viewport>
    <bounds>
    <southwest>
    <lat>55.4899270</lat>
    <lng>37.3193290</lng>
    </southwest>
    <northeast>
    <lat>56.0096570</lat>
    <lng>37.9456610</lng>
    </northeast>
    </bounds>
    </geometry>
    <partial_match>true</partial_match>
    </result>
    <result>
    <type>locality</type>
    <type>political</type>
    <formatted_address>Moscow, ID, USA</formatted_address>
    <address_component>
    <long_name>Moscow</long_name>
    <short_name>Moscow</short_name>
    <type>locality</type>
    <type>political</type>
    </address_component>
    <address_component>
    <long_name>Latah</long_name>
    <short_name>Latah</short_name>
    <type>administrative_area_level_2</type>
    <type>political</type>
    </address_component>
    <address_component>
    <long_name>Idaho</long_name>
    <short_name>ID</short_name>
    <type>administrative_area_level_1</type>
    <type>political</type>
    </address_component>
    <address_component>
    <long_name>United States</long_name>
    <short_name>US</short_name>
    <type>country</type>
    <type>political</type>
    </address_component>
    <geometry>
    <location>
    <lat>46.7323875</lat>
    <lng>-117.0001651</lng>
    </location>
    <location_type>APPROXIMATE</location_type>
    <viewport>
    <southwest>
    <lat>46.7109120</lat>
    <lng>-117.0396980</lng>
    </southwest>
    <northeast>
    <lat>46.7588820</lat>
    <lng>-116.9620680</lng>
    </northeast>
    </viewport>
    <bounds>
    <southwest>
    <lat>46.7109120</lat>
    <lng>-117.0396980</lng>
    </southwest>
    <northeast>
    <lat>46.7588820</lat>
    <lng>-116.9620680</lng>
    </northeast>
    </bounds>
    </geometry>
    <partial_match>true</partial_match>
    </result>
    </GeocodeResponse>