简体   繁体   中英

Users Location with Google Maps API Place Autocomplete

When a user starts typing in an address using the Google Maps API for Place Autocomplete, it recommends addresses near where the user is located, which they refer to as location biasing - https://developers.google.com/places/web-service/autocomplete#location_biasing

How can I determine the coordinates that the API will be using in it's location biasing when I load the autocomplete component on page load, but prior to the user typing in any address to search?

I'm not sure on how you can get these coordinates considering that the user is the one who is typing the location that you want. But, what can I help you is give an idea on how to achieve it. First you need to use the optional parameter radius to limit the return places results in the Autocomplete. Because if you did not set it, user that is in Asia can search places in the America using autocomplete. By setting this radius you can limit the places that that autocomplete can show you. You can set this radius in meters.

Now for the coordinates, you can use the Geocoding API to get the coordinates of the address that you search. Here is the sample jsfiddle for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM