Javascript 自定义谷歌地图api中的自动完成字段
我是谷歌地图api的新手,在我的项目中有一个表单&两个带有名称城市和地址的字段。我只想搜索该地址和城市内的位置。我如何才能做到这一点?最初我只选择一个字段,即位置,但绑定并没有限制搜索。 html部分:Javascript 自定义谷歌地图api中的自动完成字段,javascript,jquery,html,google-maps-api-3,Javascript,Jquery,Html,Google Maps Api 3,我是谷歌地图api的新手,在我的项目中有一个表单&两个带有名称城市和地址的字段。我只想搜索该地址和城市内的位置。我如何才能做到这一点?最初我只选择一个字段,即位置,但绑定并没有限制搜索。 html部分: div class="form-group col-md-6 col-sm-6"> <span>Location:</span> <input type="text" id="search_txt" class="form-control" placehol
div class="form-group col-md-6 col-sm-6">
<span>Location:</span>
<input type="text" id="search_txt" class="form-control" placeholder="Enter a location" />
展示你到目前为止做了什么。。检查此链接也可能会有所帮助 我在搜索时也遇到过这个问题,请检查一下 更改现有自动完成的边界 调用setBounds()更改现有数据库上的搜索区域 自动完成 //将自动完成对象偏移到用户的地理位置, //由浏览器的“navigator.geolocation”对象提供。 函数geologite(){if(navigator.geologition){ navigator.geolocation.getCurrentPosition(函数(位置){ 变量地理位置={ 纬度:位置坐标纬度, lng:position.coords.longitude }; var circle=new google.maps.circle({ 中心:地理定位, 半径:位置、坐标、精度 }); autocomplete.setBounds(circle.getBounds()); })} 基于此,为了使用autocomplete,您需要使用Google Maps JavaScript API引导URL中的
libraries
参数加载Google Places库
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
检查这个相关的SO问题:添加代码——向我们展示您迄今为止的尝试。展示您迄今为止所做的。感谢您的快速回复。我已经在我的google api中使用了libraries=places。但是我忘了在这里放置代码。还可以在autocomplete中使用位置更改。我不想以表格的形式显示地图,它只显示限制特定城市的搜索字段,只显示城市有限的位置。
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
<script src="https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY&libraries=places">
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
{types: ['geocode']});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}