Initial place for google place autocomplete

2019-09-02 14:14发布

问题:

I am using google map place autocomplete API in my application. Now at the initial load i want to show one location on that input of autocomplete. Is there any way i can achieve it without setting value of the text box ?

回答1:

Probably the API doesn't support anything like that. However, you can simulate the same by prefilling the text box with the location you want and then center your map on that location.

For example: You can prefill the textbox with 'Bangalore, Karnataka' and center the map at 12.970786, 77.593466 (Lat and Lng of Bangalore).



回答2:

If you already know the result of the auto complete, use that address/place and use either the geocoder, the places API or the resulting coordinates go display the result.

Although looking at your fiddle, I think this is what you are looking for (fill in the Autocomplete input with the predefined value of "Nebraska, United States":

<input id="autocomplete" placeholder="Enter location" type="text" value="Nebraska, United States" />

proof of concept fiddle

code snippet:

function initialize() {

  new google.maps.places.Autocomplete(
    (document.getElementById('autocomplete')), {
      types: ['geocode']
    });
}

initialize();
#autocomplete {
  width: 300px;
  padding: 7px;
}
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<input id="autocomplete" placeholder="Enter location" type="text" value="Nebraska, United States" />