Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

Google places autocomplete get the place type

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here

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:

  1. 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

  2. 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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...