I have a simple application where the user fills in the start and end point (with google places autocomplete) and then, with directions services, I draw the route on Google Maps.
I can calculate the gas needed for that route, the tolls that the driver must pay etc.
Its working fine so far.
Now I have to check if the start point or end point is an airport (I assume that I have to check the place type) and I have no idea how to achieve that.
I know that the places autocomplete object contains the place type and I was wondering if when selecting from the autocomplete I can pass to a hidden field the place type and then check if is the appropriate type.
(or any other more appropriate solution)
The predictions in place autocomplete might be a bit confusing. Let's have a look at the following example. I would like to find 'aeroport El Prat Barcelona'
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=aeroport%20El%20Prat%20Barcelona&key=YOUR_API_KEY
The first prediction has a text Aeroport, El Prat de Llobregat, Barcelona, Spain
with place Id ChIJO46GD4eepBIRIn5B25NbcNQ
. At first glance it seems to be airport, but when you check the types in the response you can see
"types":[
"transit_station","point_of_interest","establishment","geocode"
]
So this is not an airport, but the train station close to the airport as shown in the following screenshot.
At this point it looks like you cannot rely 100% on the type provided by autocomplete, because you can get something very close to the airport like the train or bus station that share the 'airport' in the name.
As a workaround I would suggest checking with places API nearby search if there is any airport close to coordinate returned by autocomplete. But this requires two additional steps:
Resolve place ID to coordinate. E.g. https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJO46GD4eepBIRIn5B25NbcNQ&key=YOUR_API_KEY
will return 41.3042622,2.0733891
Use nearby search to figure out if there is an airport in radius, for example 500 meters https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=41.3042622%2C2.0733891&radius=500&type=airport&key=YOUR_API_KEY
The last request returns the airport as shown in the following screenshot