如何在同一表格的多个输入框中添加Google自动完成功能?

时间:2018-10-27 04:34:22

标签: javascript php google-maps

我在Google端找到了一个用于自动填充地址的代码..它们仅与一个输入框链接。但我想链接同一表格上的多个输入框..请指导我。我是编码和stackoverflow的新手,所以在这里考虑我是一个tyro。

 <head>
    <title>Place Autocomplete Address Form</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">

    <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">

  </head>

  <body>
    <div id="locationField">
      <input id="autocomplete1" placeholder="Enter your address" onFocus="geolocate()" type="text">
      <input id="autocomplete2" placeholder="Enter your address" onFocus="geolocate()" type="text">
      <input id="autocomplete3" placeholder="Enter your address" onFocus="geolocate()" type="text">
    </div>



    <script>
      // This example displays an address form, using the autocomplete feature
      // of the Google Places API to help users fill in the information.

      // This example 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;


      function initAutocomplete() {
        // Create the autocomplete object, restricting the search to geographical
        // location types.
        autocomplete = new google.maps.places.Autocomplete(
            (document.getElementById('autocomplete1')),
            {types: ['geocode']});

      }


      // 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>
    <script src="https://maps.googleapis.com/maps/api/js?key=*********************&libraries=places&callback=initAutocomplete" async defer></script>
  </body>
</html>

这是我在Google上找到的示例代码。

0 个答案:

没有答案