Google多地址验证在控制台中出现错误

时间:2019-02-14 07:10:18

标签: javascript php api google-api

我正在尝试以某种形式(在大约5个地方)实现多个google address validation。我正在使用google it-self提供的基本脚本,并进行了一些小的更改。但不幸的是,它陷入了控制台中的错误

我正在调用函数onFocus并在其中传递参数,然后传递这些参数以附加搜索结果为:

地址字段

<input autocomplete="off-false" class="v-validate" type="text" id="institution-street-address" name="street-address-institution" data-validate="{required:true}" onfocus="geolocate('institution-street-address','institution-city')" />

脚本

    <script>
         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 geolocate(idMainTab,cityId) {
            console.log('hi5');
            autocomplete = new google.maps.places.Autocomplete(
            document.getElementById(idMainTab), {types: ['geocode']});
            //autocomplete.setFields('address_components');
            autocomplete.addListener('place_changed', fillInAddress(cityId));

          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());
            });
          }
        }

function fillInAddress(cityId) {
    var institutionForm = {
        locality: cityId,
    };
    console.log(institutionForm);
  // 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 then fill-in 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(institutionForm[addressType]).value = val;
    }
  }
}
       </script>

我签入了console.log(institutionForm);,我得到了传递的cityId的值。

我一直在控制台中收到此错误

  

“未捕获的TypeError:无法读取未定义的属性'address_components'”。

无法找出问题所在。

[编辑]:

我认为还有其他事情,然后提供了重复的引用,我添加了一个try catch来处理未定义的请求,但Google地址验证仍然无效:

我做了什么:

我将fillInAddress(cityId)更改为:

function fillInAddress(cityId) {
  // 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 then fill-in the corresponding field on the form.
   try {
        for (var i = 0; i < place.address_components.length; i++) {
        var addressType = place.address_components[i].types[0];
        console.log(addressType);
        if (componentForm[addressType]) {
          var val = place.address_components[i][componentForm[addressType]];
          console.log(val);
          document.getElementById(institutionForm[addressType]).value = val;
        }
      }
    } catch (e) {
        console.log('in catch');
        return e;
    }


}

现在我遇到了先前的错误。重点是我要抓住机会,但要在城市中添加地址提示到控制台中会出现此错误:

Uncaught TypeError: this.j.apply is not a function

我还发现了一些其他文章,也指出了此类错误: Passing google event data in addListener

但没有运气无法确定确切原因。

0 个答案:

没有答案