Initial place for google place autocomplete

若如初见. 提交于 2019-12-12 03:35:16

问题


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" />


来源:https://stackoverflow.com/questions/35497932/initial-place-for-google-place-autocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!