Search code examples
javascriptgoogle-maps-api-3geocodinggoogle-geocoder

How can I get detailed geocode information?


function geocodePosition(pos) {
    geocoder.geocode({
        latLng: pos
    }, function (responses) {
        if (responses && responses.length > 0) {
            updateMarkerAddress(responses[0].formatted_address);
        } else {
            updateMarkerAddress('Cannot determine address at this location.');
        }
    });
}

I can get "formatted_address" using this code. How can I get the city, country and other information?


Solution

  • It's all within the responses object that is returned from the Maps API.

    Here are the properties of the response object you're using - GeocoderResult

    It's easier to visualise the results by seeing the expanded JSON on the Geocoding page

    For example:

    responses[0].address_components would get you an array of objects containing address parts.