如何在Google地址自动完成功能中删除一些额外的详细信息?

拉胡尔·辛格(Rahul Singh)

我只想使用ID为'autocomplete'的文本框进行搜索,搜索后我不想将整个地址存储在第一个输入框中,我只想将postal_code存储在ID为自动完成的第一个输入框中,而不是ID为''的文本框中postal_code”,还有另一件事,用户也可以在ID为自动完成的文本框中手动输入postal_code。

 <!DOCTYPE html>
<html>
  <head>
    <title>Place Autocomplete Address Form</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
    <script>
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.

var placeSearch, autocomplete;
var componentForm = {
  postal_code: 'short_name'

};

function initialize() {
  // Create the autocomplete object, restricting the search
  // to geographical location types.
   var options = {

  componentRestrictions: {country: "in"}
 };

  autocomplete = new google.maps.places.Autocomplete(
      /** @type {HTMLInputElement} */(document.getElementById('autocomplete')),options);
  // When the user selects an address from the dropdown,
  // populate the address fields in the form.
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    fillInAddress();
  });
}

// [START region_fillform]
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;
    }
  }
}
// [END region_fillform]

// [START region_geolocation]
// 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 = new google.maps.LatLng(
          position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}
// [END region_geolocation]

    </script>


  </head>

  <body onload="initialize()">
    <div id="locationField">
      <input id="autocomplete" placeholder="Enter your address"
             onFocus="geolocate()"  type="text"></input>
    </div>

    <table id="address">
      <tr>
        <td class="label">Pin</td>
        <td class="slimField"><input class="field" id="postal_code"
              disabled="true" ></input></td>

      </tr>



    </table>

  </body>
</html>
施托普杰夫

试试这个代码

<html>
  <head>
    <title>Place Autocomplete Address Form</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
    <script>
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.

var placeSearch, autocomplete;
var componentForm = {
  postal_code: 'short_name'

};

function initialize() {
  // Create the autocomplete object, restricting the search
  // to geographical location types.
   var options = {

  componentRestrictions: {country: "in"}
 };

  autocomplete = new google.maps.places.Autocomplete(
      /** @type {HTMLInputElement} */(document.getElementById('autocomplete')),options);
  // When the user selects an address from the dropdown,
  // populate the address fields in the form.
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    fillInAddress();
  });
}

// [START region_fillform]
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] && addressType == "postal_code") {
          var val = place.address_components[i][componentForm[addressType]];
          document.getElementById("autocomplete").value = val;
        }
      }
}
// [END region_fillform]

// [START region_geolocation]
// 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 = new google.maps.LatLng(
          position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}
// [END region_geolocation]

    </script>


  </head>

   <body onload="initialize()">
    <div id="locationField">
      <input id="autocomplete" placeholder="Enter your address"
             onFocus="geolocate()"  type="text"></input>
    </div>

    <table id="address">
      <tr>
        <td class="label">Pin</td>
        <td class="slimField"><input class="field" id="postal_code"
              disabled="true" ></input></td>

      </tr>



    </table>

  </body>
</html>

https://jsfiddle.net/qqptf2y5/1/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Google地址自动完成中删除一些额外的细节?

来自分类Dev

jQuery自动完成功能在下拉列表中显示更多详细信息

来自分类Dev

从自动完成功能中的一个选项中检索额外的信息

来自分类Dev

从自动完成功能中的一个选项中检索额外的信息

来自分类Dev

如何从Opera地址栏自动完成功能中删除特定的URL?

来自分类Dev

如何在每行自动完成中添加额外信息

来自分类Dev

Asp.net:如何在会员数据库中添加额外的用户详细信息?

来自分类Dev

如何在“订单详细信息”页面的“订购的商品”表中显示额外字段-Magento

来自分类Dev

如何用Google的地址详细信息填充excel?

来自分类Dev

如何在ExtJS中从google.maps.places.autocomplete创建自动完成功能

来自分类Dev

如何在<h:inputText>中禁用自动完成功能?

来自分类Dev

如何在CakeStor的PHPStorm中启用自动完成功能

来自分类Dev

如何在Spyder中禁用括号自动完成功能?

来自分类Dev

如何在ajax函数中调用自动完成功能

来自分类Dev

如何在<h:inputText>中禁用自动完成功能?

来自分类Dev

如何在IntelliJ中打开自动代码完成功能?

来自分类Dev

如何在JSP中实现机场自动完成功能?

来自分类Dev

重新启动Spark后如何在Web UI中查看``完成的应用''详细信息

来自分类Dev

Firefox-删除一些地址自动填写

来自分类Dev

如何自动显示警报中的详细信息?

来自分类Dev

如何自动显示警报中的详细信息?

来自分类Dev

面板数据的滚动平均值(有一些详细信息)

来自分类Dev

React Formik Material UI自动完成功能:如何在localStorage的自动完成功能中填充值?

来自分类Dev

如何在Android中获取“当前周”详细信息以及“季度详细信息”?

来自分类Dev

如何删除单元格并在Google表格中重新输入相同的详细信息

来自分类Dev

Bash:如何检查一些操作是否成功完成?

来自分类Dev

Bash:如何检查一些操作是否成功完成?

来自分类Dev

在地址栏中禁用URL自动完成功能?

来自分类Dev

如何在python中删除一些连接的组件?

Related 相关文章

  1. 1

    如何在Google地址自动完成中删除一些额外的细节?

  2. 2

    jQuery自动完成功能在下拉列表中显示更多详细信息

  3. 3

    从自动完成功能中的一个选项中检索额外的信息

  4. 4

    从自动完成功能中的一个选项中检索额外的信息

  5. 5

    如何从Opera地址栏自动完成功能中删除特定的URL?

  6. 6

    如何在每行自动完成中添加额外信息

  7. 7

    Asp.net:如何在会员数据库中添加额外的用户详细信息?

  8. 8

    如何在“订单详细信息”页面的“订购的商品”表中显示额外字段-Magento

  9. 9

    如何用Google的地址详细信息填充excel?

  10. 10

    如何在ExtJS中从google.maps.places.autocomplete创建自动完成功能

  11. 11

    如何在<h:inputText>中禁用自动完成功能?

  12. 12

    如何在CakeStor的PHPStorm中启用自动完成功能

  13. 13

    如何在Spyder中禁用括号自动完成功能?

  14. 14

    如何在ajax函数中调用自动完成功能

  15. 15

    如何在<h:inputText>中禁用自动完成功能?

  16. 16

    如何在IntelliJ中打开自动代码完成功能?

  17. 17

    如何在JSP中实现机场自动完成功能?

  18. 18

    重新启动Spark后如何在Web UI中查看``完成的应用''详细信息

  19. 19

    Firefox-删除一些地址自动填写

  20. 20

    如何自动显示警报中的详细信息?

  21. 21

    如何自动显示警报中的详细信息?

  22. 22

    面板数据的滚动平均值(有一些详细信息)

  23. 23

    React Formik Material UI自动完成功能:如何在localStorage的自动完成功能中填充值?

  24. 24

    如何在Android中获取“当前周”详细信息以及“季度详细信息”?

  25. 25

    如何删除单元格并在Google表格中重新输入相同的详细信息

  26. 26

    Bash:如何检查一些操作是否成功完成?

  27. 27

    Bash:如何检查一些操作是否成功完成?

  28. 28

    在地址栏中禁用URL自动完成功能?

  29. 29

    如何在python中删除一些连接的组件?

热门标签

归档