2

We are using the Google Geocoding API to validate UK postcodes on our transport booking form however it doesn't always return the street address, example EC2Y 8DR a London postcode will return the long/lat but the booing form fields are not populated. We moved away from a paid service and now our clients have a serious problem entering bookings.

You can see this here https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform

Is there a work around?

Thanks in advance Simon

xomena
  • 31,125
  • 6
  • 88
  • 117
SimonGare
  • 21
  • 1
  • 2
  • Could it be that the postcodes you are having problems with cover more than one street / location? The example postcode EC2Y 8DR covers a City of London Police Station as well as Shakespeare Towers. – RobertKenny Apr 01 '16 at 08:10
  • Does your code assume there will be a street name? That postcode is for the Barbican in London which doesn't have a street name in its Royal Mail address. Eg, Flat 11 Shakespeare Tower Barbican LONDON EC2Y 8DR – OTTA Apr 01 '16 at 08:11
  • Are you trying to get a street back from submitting a postcode? Or are you trying to submit a full address with postcode and validate that the postcode matches that address? – camiblanch Apr 01 '16 at 16:51

1 Answers1

3

Let's have a look at an autocomplete request for 'EC2Y 8DR'

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=EC2Y%208DR&key=YOUR_API_KEY

If you execute this request, you will see that suggestion 'London EC2Y 8DR, United Kingdom' has a type "postal_code" (place ID ChIJgYd44lUbdkgRUMGemVRu-1Q). So when you request details for this place ID you will get back information about a postal code itself which doesn't contain any street name.

https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJgYd44lUbdkgRUMGemVRu-1Q&key=YOUR_API_KEY

You can see the postal code in Map Maker: http://www.google.com/mapmaker?gw=90&iwloc=0_0&dtab=history&cid=6123609427973947728

The workaround is the following:

When you request a place detail analyze the type. If the type is "postal_code" extract the latitude, longitude of the postal code and execute reverse geocoding to obtain the nearest street address.

In your example the postal code has coordinates 51.5202776,-0.09492260000000001. The reverse geocoding will return an address '331 Shakespeare Tower, London EC2Y 8DR, UK'.

Please look at the result in Geocoder tool:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D51.520278%252C-0.094923

xomena
  • 31,125
  • 6
  • 88
  • 117