Javascript 通过谷歌API的两个自动完成字段

Javascript 通过谷歌API的两个自动完成字段,javascript,jquery,google-api,Javascript,Jquery,Google Api,我有两个字段,分别是id=“捡拾位置”和id=“放置位置”。我想用印度城市的名字自动完成这些字段 <input type="text" value="" name="form[Pickup Location]" id="Pickup Location" onfocus="geolocate()" class="rsform-input-box"> <input type="text" value="" name="form[Drop Location]" id="Drop L

我有两个字段,分别是
id=“捡拾位置”
id=“放置位置”
。我想用印度城市的名字自动完成这些字段

<input type="text" value="" name="form[Pickup Location]" id="Pickup Location" onfocus="geolocate()" class="rsform-input-box">

<input type="text" value="" name="form[Drop Location]" id="Drop Location" onfocus="geolocate()" class="rsform-input-box">

代码需要做很多修改,因此不是一个代码编写服务,所以我将保留它的基本功能

函数需要html元素来存储两个位置的值。由于这些元素不存在,我已经删除了该代码

geologite()
函数看起来仅用于在地图上打印,而地图在代码中不存在,因此我将其删除

因此,以下内容仅适用于自动完成。你仍然需要弄清楚如何通过谷歌地图API只返回印度城市

var placeSearch、pickupLocation、dropLocation;
函数initAutocomplete(){
//创建自动完成对象,将搜索限制为地理位置
//位置类型。
pickupLocation=new google.maps.places.Autocomplete(
/**@type{!HTMLInputElement}*/
(document.getElementById(“皮卡位置”){
类型:['geocode']
});
dropLocation=new google.maps.places.Autocomplete(
/**@type{!HTMLInputElement}*/
(document.getElementById('Drop_Location')){
类型:['geocode']
});
}

该代码需要进行大量更改,因此不是一项代码编写服务,因此我将保留它的基本功能

函数需要html元素来存储两个位置的值。由于这些元素不存在,我已经删除了该代码

geologite()
函数看起来仅用于在地图上打印,而地图在代码中不存在,因此我将其删除

因此,以下内容仅适用于自动完成。你仍然需要弄清楚如何通过谷歌地图API只返回印度城市

var placeSearch、pickupLocation、dropLocation;
函数initAutocomplete(){
//创建自动完成对象,将搜索限制为地理位置
//位置类型。
pickupLocation=new google.maps.places.Autocomplete(
/**@type{!HTMLInputElement}*/
(document.getElementById(“皮卡位置”){
类型:['geocode']
});
dropLocation=new google.maps.places.Autocomplete(
/**@type{!HTMLInputElement}*/
(document.getElementById('Drop_Location')){
类型:['geocode']
});
}

根据您的要求,以下代码适用于您!确保你写的表格填写程序正常

函数initAutocomplete(){
var myFirstEle=document.getElementById('firstEle'),
mySecondEle=document.getElementById('secondEle');
生成完成(myFirstEle);
generateAutoComplete(mySecondEle);
}
函数生成器自动完成(ele){
//创建自动完成对象,将搜索限制为地理位置
//位置类型。
var autocomplete=new google.maps.places.autocomplete(
/**@type{!HTMLInputElement}*/
埃勒{
类型:['geocode']
});
//当用户从下拉列表中选择地址时,填充该地址
//表单中的字段。
autocomplete.addListener('place_changed',fillInAddress.bind(autocomplete));
}
函数fillInAddress(自动完成){
//从自动完成对象获取位置详细信息。
var place=autocomplete.getPlace();
for(componentForm中的var组件){
document.getElementById(组件).value='';
document.getElementById(组件).disabled=false;
}
//从place details中获取地址的每个组件
//并在表单上填写相应的字段。
对于(变量i=0;i}
根据您的要求,以下代码适用于您!确保你写的表格填写程序正常

函数initAutocomplete(){
var myFirstEle=document.getElementById('firstEle'),
mySecondEle=document.getElementById('secondEle');
生成完成(myFirstEle);
generateAutoComplete(mySecondEle);
}
函数生成器自动完成(ele){
//创建自动完成对象,将搜索限制为地理位置
//位置类型。
var autocomplete=new google.maps.places.autocomplete(
/**@type{!HTMLInputElement}*/
埃勒{
类型:['geocode']
});
//当用户从下拉列表中选择地址时,填充该地址
//表单中的字段。
autocomplete.addListener('place_changed',fillInAddress.bind(autocomplete));
}
函数fillInAddress(自动完成){
//从自动完成对象获取位置详细信息。
var place=autocomplete.getPlace();
for(componentForm中的var组件){
document.getElementById(组件).value='';
document.getElementById(组件).disabled=false;
}
//从place details中获取地址的每个组件
//并在表单上填写相应的字段。
对于(变量i=0;i}
你尝试了什么?我不能用JS编码,所以我能做的就是将id从
取货地点更改为
自动完成。。。好啊你有运行代码的小提琴或公共网页吗?你尝试了什么?我不能用JS编码,所以我能做的最好是将id从
拾取位置更改为
自动完成。。。好啊你有没有一个小提琴或一个公共网页,你可以
<input id="autocomplete" onFocus="geolocate()" type="text">

<script>
      // This example displays an address form, using the autocomplete feature
      // of the Google Places API to help users fill in the information.

      // This example requires the Places library. Include the libraries=places
      // parameter when you first load the API. For example:
      // <script src="https://maps.googleapis.com/maps/api/js?key=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;
        }

        // Get each component of the address from the place details
        // and fill the corresponding field on the form.
        for (var i = 0; i < place.address_components.length; i++) {
          var addressType = place.address_components[i].types[0];
          if (componentForm[addressType]) {
            var val = place.address_components[i][componentForm[addressType]];
            document.getElementById(addressType).value = val;
          }
        }
      }

      // Bias the autocomplete object to the user's geographical location,
      // as supplied by the browser's 'navigator.geolocation' object.
      function geolocate() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var geolocation = {
              lat: position.coords.latitude,
              lng: position.coords.longitude
            };
            var circle = new google.maps.Circle({
              center: geolocation,
              radius: position.coords.accuracy
            });
            autocomplete.setBounds(circle.getBounds());
          });
        }
      }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyADbpfJGWwRh5um_gd2gNySgrI5FAy2RZk&libraries=places&callback=initAutocomplete" async defer></script>