I need to get a representative Canadian postal code based on city and province name. My initial approach has been to use Google's geocoding API to convert city, province into [lat,lng] reverse geocode that to get the matching address, and extract the postal code from that. The city and province are coming from a form that uses the Google Maps autocomplete, so I know that they are (at least nearly) always going to be valid. In some tests, this is working fine, but I quickly ran into a case in our actual data that fails: Snow Lake, Manitoba.
https://maps.googleapis.com/maps/api/geocode/json?address=Snow+Lake%2C+Manitoba&key=MYKEY
In the geometry section, I get:
location    
    lat 54.87856679999999
    lng -100.0220321
location_type   "APPROXIMATE"
But whenever I do a lookup of that location (including varying levels of precision), I get back results that don't include a postal code.
https://maps.googleapis.com/maps/api/geocode/json?latlng=54.8785668,-100.0220321&key=MYKEY
I've also tried this with the place_id that gets returned from various things, but no luck. Strange thing is, if I search for Snow Lake, Manitoba in the regular Google Maps site, it pinpoints it and tells me the postal code is R0B 1M0. So why does their site give good info, but not the API? Even if I replace the location in my second URL with the location that Google Maps has in the URL following my search (54.8808471,-100.0274579), I still don't get back a postal code, nor do I if I use an address that it gives for that location (531 Lakeshore Drive).
Through trial and error (looking at the Google Map for some street names), I found that searching 117 Balsam St. gives both a postal code and a location that I can reverse geocode to get a postal code. But my incoming data doesn't have street names or numbers, just city and province, so this isn't a solution. This address does give me ROOFTOP for the location_type, not APPROXIMATE or RANGE_INTERPOLATED that I get for some others.
Anyone know what the deal is here? Is there a reliable method that I can use to get what I need from this API, or is it simply going to be a hit-and-miss proposition?
I believe the idea to search a postal code using the reverse geocoding is generally correct, but there might be certain situations when it doesn't work. Let me explain with examples.
https://www.google.com/maps/place/Montr%C3%A9al-Nord,+QC+H1H,+Canada/@45.5755194,-73.6272394,14z/data=!4m5!3m4!1s0x4cc91f6dd2d3b269:0xc3ff559c136d8af2!8m2!3d45.5893471!4d-73.6419587
 
 
So if you specify a point inside this polygon, for example 45.594402,-73.642216, and execute reverse geocoding specifying also that the result type should be a postal code, you will get a result
https://maps.googleapis.com/maps/api/geocode/json?latlng=45.594402%2C-73.642216&result_type=postal_code&key=YOUR_API_KEY
https://www.google.com/maps/place/Snow+Lake,+MB+R0B+1M0,+Canada/@54.8792581,-100.0226575,17.04z/data=!4m5!3m4!1s0x52f631c0d3937ca9:0x143db2e5b7611e34!8m2!3d54.880844!4d-100.0252639
 
 
Due to this reason the reverse geocoding is failing and returns ZERO_RESULTS.
https://maps.googleapis.com/maps/api/geocode/json?latlng=54.87856679999999%2C-100.0220321&result_type=postal_code&key=YOUR_API_KEY
I believe this is a data issue on Google side and you can report it to Google following the Google Maps Help:
https://support.google.com/maps/answer/3094088
I can suggest the following workaround in case when the postal code doesn't have associated polygon geometry:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=54.87856679999999%2C-100.0220321&radius=2000&type=post_office&key=YOUR_API_KEY
The request returns postal office with place ID ChIJp3yT08Ax9lIRk0eG4BkTvXg.
https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJp3yT08Ax9lIRk0eG4BkTvXg&key=YOUR_API_KEY
Extract postal code from address components of the post office
{
    "address_components":[
    {
      "long_name":"81",
      "short_name":"81",
      "types":[
        "street_number"
      ]
    },
    {
      "long_name":"Balsam Street",
      "short_name":"Balsam St",
      "types":[
        "route"
      ]
    },
    {
      "long_name":"Snow Lake",
      "short_name":"Snow Lake",
      "types":[
        "locality","political"
      ]
    },
    {
      "long_name":"Division No. 21",
      "short_name":"Division No. 21",
      "types":[
        "administrative_area_level_2","political"
      ]
    },
    {
      "long_name":"Manitoba",
      "short_name":"MB",
      "types":[
        "administrative_area_level_1","political"
      ]
    },
    {
      "long_name":"Canada",
      "short_name":"CA",
      "types":[
        "country","political"
      ]
    },
    {
      "long_name":"R0B 1M0",
      "short_name":"R0B 1M0",
      "types":[
        "postal_code"
      ]
 }
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With