jrdioko
jrdioko

Reputation: 33525

Making sense of Google Geocoding API results

I'm using Google's Geocoding API to process free-form address data and am trying to make sense of the results (subpremise, administrative_area_level_2, etc.). At this point I'm only interested in addresses in the US, and would like to format the results as a street address, city, and state code. The state code seems straightforward (administrative_area_level_1), but the others are more vague. Is city locality or sublocality or both or even more? The street address itself seems like it could be any number of combinations of the other fields.

Ideally, I'd like to just take the formatted_address, strip off the city, state, zip, and country code, and keep what is left as my "street address". Are there any guidelines or recommendations for handling all these fields, at least for the majority of cases (regular addresses, addresses with unit numbers, etc.)?

Upvotes: 7

Views: 5615

Answers (5)

IfYouSeekAnthony
IfYouSeekAnthony

Reputation: 348

After many hours of working on it, I wasn't able to consistently extract the flat / room / sub premise. Having that detail was more important than being able to specifically separate it, so I got around it using this.

var componentForm = {
    locality: 'long_name',
    administrative_area_level_1: 'short_name',
    country: 'long_name',
    postal_code: 'short_name'
};

function fillInAddress(place) {
    for (var component in componentForm) {
        document.getElementById(component).value = '';
        document.getElementById(component).disabled = false;
    }
    for (var i = 0; i < place.address_components.length; i++) {
        var addressType = place.address_components[i].types[0];
        if (componentForm[addressType]) {
            var val = place.address_components[i][componentForm[addressType]];
            document.getElementById(addressType).value = val;
        }
    }
    var parts = $("#searchField").val().split(",");
    $("#street_number").val(parts[0]);
}

I've extracted the information from the auto complete as normal, although skipped "street_number" and "route". Then did a simple split on the auto complete value after selection and used the first portion, returning the street address as a whole (including factory / flat / premise and street name).

This work around gave me results that work well for what I need. Good luck!

Upvotes: 0

Sean
Sean

Reputation: 341

  • Street address is street_number and route
  • City is locality
  • State is administrative_area_level_1
  • ZIP is postal_code
  • Country is country

Note that Google ignores any kind of unit number (e.g. 'Apt 13A'). You'll have to add that back in yourself.

Running a sample of your data through the geocoder and checking the results manually should confirm that you're getting what you need.

Upvotes: 8

Mullins
Mullins

Reputation: 2364

I think you need to use 'route' for the street address.

I use the same service and from my experience 'route' is always the equivalent of a street address, even when the location is not actually a street!

I think you have already determined how to figure out the city name. etc. It is the same approach I take; have a look at a variety of API results for a few different locations and see which field gives you the most consistent results.

Upvotes: 0

rndapology
rndapology

Reputation: 152

I am doing reverse geocoding on Nearlots.com in the search pages. Basically, the user drops a marker somewhere on the map and I print an address in a search box.

I am simply printing 'formatted_address' and giving up if it's not there. This will give you something like this: "275-291 Bedford Ave, Brooklyn, NY 11211, USA". This is more than sufficient - you can always strip out the USA at the end.

Upvotes: 1

expelledboy
expelledboy

Reputation: 2383

The documentation seems to cover what the various fields mean quite clearly.

You havent said what language you are working in, but I think this will help.

Upvotes: 1

Related Questions