【问题标题】:How to remove some extra detail in google address auto-complete?如何删除谷歌地址自动完成中的一些额外细节?
【发布时间】:2015-05-14 07:10:38
【问题描述】:

我只想使用带有 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>

【问题讨论】:

  • 嗨,Rahul,你能给我在印度的任何地址(带有门牌号)吗?我只需要看一个例子。
  • 我错了,现在我改变了问题。实际上我想搜索地址并将地址的一部分存储在同一个文本框中。请用代码解释我。

标签: javascript html google-search-api


【解决方案1】:

试试这个代码

<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/

【讨论】:

    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2018-08-28
    • 2021-04-12
    相关资源
    最近更新 更多