GuidoG
GuidoG

Reputation: 12014

google directions API keeps changing the destination postalcode

I am having troubles using the google directions api.

for example I want to retreive the distance between two cities, I send the following command :

https://maps.googleapis.com/maps/api/directions/json?origin=8380 Belgium&destination=2030 Belgium&sensor=false&alternatives=true&key=<mykey>

it should return details about the route between postalcode 8380 (belgium) and postalcode 2030 (belgium).

But in the result I see that google had altered postalcode 2030 into 4730 !
That is a complete different location.

See also this enter image description here

If i simply use the maps url like this

http://www.google.be/maps/dir/8380 Belgium/2030 Belgium

then it works as expected.

How can I force google to NOT change the postal code into something else but keep using the postal code i send ?

EDIT
I have been playing with it and it turns out that if I use B-2030 in stead of 2030 as postalcode, it works as expected.
If I use BE-2030 it gets me close but still 15 km to far.
But using a prefix like B- on all postalcodes is something I rather not do, maybe it will work wrong on some other postal codes, and we also have a lot of foreign addresses I dont want to keep a list of all country codes...

EDIT
I had put this question as a ticket with google, I received an answer that they are aware of this issue and the developers are looking for a fix.
I will update this post when I receive more news from them.

Upvotes: 0

Views: 637

Answers (1)

xomena
xomena

Reputation: 32138

You are right this is a known issue with 4-digit postal codes that was reported in Google issue tracker for a bunch of countries:

https://issuetracker.google.com/issues/75985322

Hopefully Google will solve it soon.

In the meantime it looks like the only reliable workaround consists in resolving the postal codes to their places IDs via place autocomplete service and applying place IDs in directions requests.

E.g. execute the place autocomplete requests for your examples:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=8380%20Belgium&types=(regions)&key=YOUR_API_KEY

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=2030%20Belgium&types=(regions)&key=YOUR_API_KEY

These requests will return place ID ChIJo0saJPpVw0cRzkf6DZ5gRBo for '8380 Belgium' and place ID ChIJ4ejQlRcIxEcRnGOzGMCkbYc for '2030 Belgium'.

So, now you can execute directions request with these place IDs and get expected results

https://maps.googleapis.com/maps/api/directions/json?origin=place_id%3AChIJo0saJPpVw0cRzkf6DZ5gRBo&destination=place_id%3AChIJ4ejQlRcIxEcRnGOzGMCkbYc&mode=driving&alternatives=true&key=YOUR_API_KEY

Here is the same request in directions calculator:

https://directionsdebug.firebaseapp.com/?origin=place_id%3AChIJo0saJPpVw0cRzkf6DZ5gRBo&destination=place_id%3AChIJ4ejQlRcIxEcRnGOzGMCkbYc&mode=driving&alternatives=true

I hope this workaround might be helpful.

Upvotes: 1

Related Questions