Search code examples
apigoogle-mapsgoogle-apipostal-code

Google map api returns Notfound result for valid postal-cod for distance calculation


We are using Google map api to find distance between two postalcodes. We can get the distance between any two postalcodes by using google map api Working Map API example. Some of the postal-code this api returns NotFound error when the postal-code is sent without any space, but for the same postal-code if we give space after 3 letters it works well and returns the distance correctly.

API URL

http://maps.googleapis.com/maps/api/distancematrix/json?origins=H3W3C4&destinations=H1C0A6&mode=driving&language=en-EN&sensor=false

Example 1:

url:http://maps.googleapis.com/maps/api/distancematrix/json?origins=H3W3C4&destinations=H1A0C2&mode=driving&language=en-EN&sensor=false postal-code: H1A0C2, api result : NotFound

Example 2:

url:http://maps.googleapis.com/maps/api/distancematrix/json?origins=H3W3C4&destinations=H1A 0C2&mode=driving&language=en-EN&sensor=false postal-code: H1A 0C2,api result : returns correct distance

Below are list of postal codes for which api returns NotFound if we give postal-code without space(but if we give space like 'H1A 0C2' it will return results)

H1A0C2 H1A0C3 H1A0C4 H1B0B7 H1C0E3 H1C0E4 H1C0E5 H1C0E6 H1C0E7 H1C0E8 H1C0E9 H1C0G1 H1C0G2

below list postalcodes for which api returns the distance correctly if we give with/without space( like works well if we give 'H1C 0A7' and 'H1C0A7'.

H1C0A7 H1C0A8 H1C0A9

Though it works with or without space for most of the postalcodes, for few it does not return values without spaces. What could be the reason?


Solution

  • I would suggest checking if the postal code exists in the Google database.

    For example the postal code H1A0C2 seems to be missing

    https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/utils/geocoder/#q%3D%26options%3Dtrue%26in_country%3DCA%26in_postal_code%3DH1A0C2

    As you can see, the geocoder tool returns only postal code prefix HA1, but not the postal code itself.

    For the postal code H1C0A9 geocoder returns a complete postal code:

    https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/utils/geocoder/#q%3D%26in_country%3DCA%26in_postal_code%3DH1C0A9

    I think the distance matrix cannot find distance for missing postal codes. However, when you add an empty space it can find a postal code prefix and calculates distance based on coordinates of postal code prefix. So the result might be not precise enough in this case.

    You can report missing postal codes to Google as described in the following support doc:

    https://support.google.com/maps/answer/3094088

    Hope this helps!