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