【问题标题】:Google Place Autcomplete Address API forces HTML Autocomplete = OffGoogle Place Autocomplete Address API 强制 HTML Autocomplete = Off
【发布时间】:2019-03-15 18:36:00
【问题描述】:

我们有一个标准的地址表格(Street 1、Street 2、City、State、Zip、Country)。

当我在 Street 1 上添加 Google Places Autocomplete API 的代码时,它会在加载页面时自动将输入字段自动更改为关闭,这使得 HTML/Browser Autofills 在 Street 1 上不起作用。

<input autocomplete="address-line1" type = "text" name = "shipping_address_1" id = "shipping_address_1"   class="ct-required form-control" tabindex=7>

关于如何纠正此问题的任何想法;为什么 Google Place Autocomplete API 会将字段更改为 autocomplete='off'?

我尝试将 Google javascript 变量“autocomplete”修改为“autocomp”,认为这与此有关,但结果仍然相同。

      var placeSearch, autocomp;
  var componentForm = {
    shipping_address_1: 'short_name',
    shipping_address_2: 'long_name',
    shipping_city: 'long_name',
    shipping_state: 'short_name',
    shipping_country: 'long_name',
    shipping_zip: 'short_name'
  };



  function initAutocomplete() {
    // Create the autocomplete object, restricting the search to geographical
    // location types.
    autocomp = new google.maps.places.Autocomplete(
        /** @type {!HTMLInputElement} */(document.getElementById('shipping_address_1')),
        {
            types: ['geocode'],
            componentRestrictions: {country: "us"}
        });

    // When the user selects an address from the dropdown, populate the address
    // fields in the form.
    autocomp.addListener('place_changed', fillInAddress);
  }

  function fillInAddress() {
    // Get the place details from the autocomplete object.
    var place = autocomp.getPlace();
    //debug autofillinplaes
    console.log(place);


    for (var component in componentForm) {
      document.getElementById(component).value = '';
      document.getElementById(component).disabled = false;
    }

if (place.address_components) {
    // Get each component of the address from the place details
    // and fill the corresponding field on the form.

    var components_by_type = {};

    for (var i = 0; i < place.address_components.length; i++) {
      var addressType = place.address_components[i].types[0];
      var c = place.address_components[i];
      components_by_type[c.types[0]] = c;

    }
     var streetnumb = components_by_type['street_number'].short_name;

          var address1 = streetnumb+" "+components_by_type['route'].long_name;
            document.getElementById('shipping_address_1').value = address1;

         var locality = components_by_type['locality'].long_name;     
        document.getElementById('shipping_city').value = locality;

         var administrative_area_level_1 = components_by_type['administrative_area_level_1'].short_name;      
        document.getElementById('shipping_state').value = administrative_area_level_1;

          var country = components_by_type['country'].short_name;             
        document.getElementById('shipping_country').value = country;

          var postal_code = components_by_type['postal_code'].short_name;         
        document.getElementById('shipping_zip').value = postal_code;
... [DO ADDRESS VALIDATION, UPDATE INPUT FIELDS] ...}

【问题讨论】:

  • 对于大多数通过 JavaScript 实现的自动完成功能来说,这是正常的做法,因为可能会出现多个建议列表,因此可能会给最终用户造成混淆。
  • 如果您已经使用了另一个自动完成插件,为什么还要打开它?
  • @plalx 这是电子商务网站的送货地址表格。以名字开头,浏览器自动完成功能出现,这对最终用户来说非常方便。如果用户在没有使用浏览器自动完成(预填充)的情况下到达街道地址 1,则在开始输入时使用 Google 自动完成 API。
  • 你能做一个简单的sn-p来重现这个问题吗?
  • @ToddN 不过,名字将在与地址不同的字段中输入,对吧?您的意思是整个表单都禁用了自动完成功能吗?

标签: javascript google-maps google-maps-api-3 autocomplete google-places-api


【解决方案1】:
autocomp = new google.maps.places.Autocomplete(
document.getElementById('shipping_address_1')),
        {
            types: ['geocode'],
            componentRestrictions: {country: "us"}
        });

通过编写上述代码,您是在告诉 GMaps 这是您需要预测位置的字段。它有点像地方搜索栏,任何类型的搜索栏都不会自动完成。如果由浏览器自动填充,那将没有任何意义。

如果您希望这样,请使用 WebService API,发送 HTTP 请求并使用其响应填充字段。

从文档中, 在以加利福尼亚州旧金山为中心的区域内包含字符串“Amoeba”的场所请求: https://maps.googleapis.com/maps/api/place/autocomplete/xml?input=Amoeba&amp;types=establishment&amp;location=37.76999,-122.44696&amp;strictbounds&amp;key=YOUR_API_KEY 使用查询参数input=Paris&amp;types=geocode 对上述 URL 的 GET 请求将给出一个包含预测的 JSON 对象。

{
  "status": "OK",
  "predictions" : [
      {
         "description" : "Paris, France",
         "id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
         "matched_substrings" : [
            {
               "length" : 5,
               "offset" : 0
            }
         ],
    ... more results
    ]
} 

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 2020-08-10
    • 2017-01-15
    • 2019-04-11
    • 2013-08-10
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    相关资源
    最近更新 更多