0
votes

I try to create a google maps API call that searches an address in a specific boundary.

So in my test I want to search for "8670" which is a postal code from a village in Belgium. This village: https://www.google.com/maps/place/8670+Koksijde,+Belgium/@51.1129798,2.6459201,13z/data=!3m1!4b1!4m5!3m4!1s0x47dcba8d702c832d:0x10129f1eb06df0d2!8m2!3d51.117577!4d2.6744402

Using the country-parameter in the API call gives me the right answer: https://maps.googleapis.com/maps/api/geocode/json?address=8670|country:BE

But using the bounds-parameter returns me a city in the USA: https://maps.googleapis.com/maps/api/geocode/json?address=8670&bounds=53.6014224,7.0979623|49.4057287,2.4896213

I know bounds is not a strict filter, but as there is a city in range of the bounds with that postal code leaves me with the question why does google not find it and return me a city far away from my bounds?

The reason why I want to use bounds and not country-parameter is because the search is for 3 countries (NL, BE, LU) and as far as I know it is not possible to use OR in the country-parameter.

1

1 Answers

1
votes

The documentation says that bounds parameter defines the latitude/longitude coordinates of the southwest and northeast corners of this bounding box.

https://developers.google.com/maps/documentation/geocoding/intro#Viewports

So, the first coordinate should be coordinate of southwest and second coordinate should be coordinate of northeast. In you example you define bounds in opposite order NE|SW. Just swap values in bounds parameter and you will have expected result '8670 Koksijde, Belgium' (place ID ChIJLYMscI263EcR0vBtsB6fEhA):

https://maps.googleapis.com/maps/api/geocode/json?address=8670&bounds=49.4057287%2C2.4896213%7C53.6014224%2C7.0979623&key=YOUR_API_KEY

or

https://maps.googleapis.com/maps/api/geocode/json?components=postal_code%3A8670&bounds=49.4057287%2C2.4896213%7C53.6014224%2C7.0979623&key=YOUR_API_KEY

I hope this helps!