放置API自动填充表单字段不起作用

时间:2019-02-07 13:43:17

标签: javascript wordpress autocomplete google-places-api

我已经在WordPress网站上创建了一个测试文件,以尝试找出答案并通过地方信息库学习Google Maps Api。关于基于SO的Google Maps API的不同问题,有很多问题,但是我还没有找到解决我问题的答案。

我收到setbounds()的未捕获错误消息,并且如果我尝试调整代码,它只会引发另一个错误,例如“未定义google”,“未定义地理信息”等,这是一个永无止境的流错误。

不用说,我对javascript的经验非常有限,仅涉及到简单的“ show()”,“ hide()”函数,仅此而已。所以我很难调试这个。

我已经从https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform复制了确切的代码。我将添加我的代码,以便您可以看到它的外观并希望为您提供答案。

注意1:我在自己的文件中为maps api place库和jQuery添加了单独的脚本标签,只是为了查看是否有所作为,但随后与我的enq Maps冲突了。在functions.php文件中的脚本。 如果我在functions.php中注释掉enq脚本,而仅在文件/页面中使用“ script src”,则会引发与enq脚本相同的错误。

注意2:我也尝试过将JS包装在“ script src(maps api)”标签内,这根本没有引起任何变化,无论哪种方式都引发了相同的错误。

有效方法和无效方法:无效,单击“自动完成”字段会抛出不同的未定义错误,并且不提供任何自动完成下拉菜单。

我的代码:

functions.php(排队脚本)

    wp_enqueue_script('googleMap', '//maps.googleapis.com/maps/api/js?key=KEY&libraries=places', NULL, '1.0', true);

page-custom.php(标记)

<form>
    <div class="form-group">
                    <input type="text" class="form-control" id="autocomplete" placeholder="Search Address" onFocus="geolocate()">
    </div>
    <div class="form-group">
                    <input type="text" class="form-control" id="route" placeholder="Street" disabled>
    </div>                    
    <div class="form-group">
                    <input type="text" class="form-control" id="street_number" placeholder="Nr" disabled>
    </div>                    
    <div class="form-group">
                    <input type="text" class="form-control" id="postal_code" placeholder="Zip" disabled>
    </div>                    
    <div class="form-group">
                    <input type="text" class="form-control" id="locality" placeholder="City" disabled>
    </div>
    <div class="form-group">
                    <input type="text" class="form-control" id="administrative_area_level_1" placeholder="County" disabled>
    </div>
    <div class="form-group">
                    <input type="text" class="form-control" id="country" placeholder="Country" disabled>
    </div>
</form

page-custom.php(JS“置于标记下方”)

<script>
    // This sample uses the Autocomplete widget to help the user select a
    // place, then it retrieves the address components associated with that
    // place, and then it populates the form fields with those details.
    // This sample 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 predictions to
      // geographical location types.
      autocomplete = new google.maps.places.Autocomplete(
          document.getElementById('autocomplete'), {types: ['geocode']});

      // Avoid paying for data that you don't need by restricting the set of
      // place fields that are returned to just the address components.
      autocomplete.setFields('address_components');

      // When the user selects an address from the drop-down, 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 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(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>

如果我删除了所有JS并把这段代码删除了,我在一个与此处其他内容有关的问题中发现了该代码,那么我将使自动完成地址搜索输入起作用,但是该代码段仅具有该功能,而没有填充其他任何字段。

这是有效的代码段(仅用于地址搜索)

<script>
jQuery(document).ready(function ($) {
    $(function(){
      var autocomplete;
      var geocoder;
      var input = document.getElementById('autocomplete');
      var options = {
        types: ['geocode']
      };
      autocomplete = new google.maps.places.Autocomplete(input,options);
    });
});
</script>

在这里希望有人帮助解决这个问题,因为我不知道该怎么做才能使它正常工作。

0 个答案:

没有答案