11

I'm using Google Maps V3 api. I am submitting an address search to return the proper geocoded result including the address, name of establishment, and lat/lngs.

My problem is that the response from the geocoder can be formatted differently. It always follows the same structure, but some responses use different keys for the address_components data structure.

For Example, some searches result in:

establishment               -> location name
street_number               -> address street number
route                       -> the street name
locality                    -> the city
administrative_area_level_1 -> the state
postal_code                 -> zip/postal code

whereas, if i were to search a general area, such as "Hampton Beach, NH" i would receive:

sublocality                 -> beach name / area
administrative_area_level_3 -> city/town name
administrative_area_level_1 -> the state
postal_code                 -> zip/postal code

as you can see the two responses have their differences. Is there a known jquery library that can be used to handle these different responses to return a data set of the address components that can be used for a human-readable way?

I will note that the response also returns a "formatted_address" type, which returns it like: "Hampton Beach, NH 03842, USA" OR "Boston University, 1 University Rd, Boston, MA 02215-1407, USA" As you can see, these too are pretty different. I could split by comma, but I'd like to use the actual address_components for a flawless database insert.

2 Answers 2

15

Why not have your DB mirror only the following keys?

street_number                    -> address street number
route                            -> the street name
locality                         -> the city/town    
administrative_area_level_3      -> the city/town
administrative_area_level_1      -> the state
postal_code                      -> zip/postal code

Where a locality exists, use that in your request (as it appears to deliver the more detailed info - http://code.google.com/apis/maps/documentation/geocoding/#JSON )

Where no street name or locality exists, request administrative_area_level_3 and administrative_area_level_1

This would provide you with a full human-readable postal address when the info exists or just the city/state for a sublocality (e.g. beach), as you mentioned in one of the comments.

**I'm assuming you only care about the US.

3
  • In general, the city could be locality or post_town or any of the administrative_area_level_X or even the country (Singapore, Hong Kong, Vatican City, etc.). Just look at all of them, in that order (administrative_area_level_X for X from 5 down to 1) and you'll catch the city wherever it is.
    – miguev
    Jul 14, 2016 at 14:56
  • @miguev is right that the city can be in any of those fields, even inside US I have seen the city in area_level_1 which is supposed to be the state). However you can not just parse down you'll get states and provinces as cities in many cases which is likely worse than having NO city at all.
    – John
    Dec 24, 2018 at 6:06
  • just to add neighborhood is also town/city, perhaps added later than 2018 Jan 19, 2023 at 9:55
1

Addresses are very much a 'human' thing. I think the reason Google's data is so messy is because their source data is messy, but likely similar for one area.

Which parts of the address are relevant to you? If you're just feeding this to a human just give him whatever you have?

3
  • 1
    I'm aiming for a full postal address. For places like a beach, i'd like just the city/state. The problem is that city/town is referred differently for different requests. I need to extract each component of the address like how they provide, it's just too messy to get it generic enough for any type of request, ya kno?
    – Atticus
    Jun 10, 2011 at 22:33
  • This seems very tricky. There isn't really a way to interpret what 'administrative_area' is, it can be a state, substate or city and sometimes it's not even there. The documentation does specify which values you can expect: code.google.com/apis/maps/documentation/geocoding/#Types but I'm guessing they're all optional. Maybe you need to look at a commercial system?
    – Halcyon
    Jun 10, 2011 at 22:51
  • Yeah man I'm thinking the same.. I was hoping someone perhaps knows of a jquery library to filter the components into generic [address, city, state, zip] terms. As you said, administrative_area may differ, so the generic use of this seems pretty limited.
    – Atticus
    Jun 10, 2011 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.