仅在没有单选按钮的情况下自动完成位置

时间:2012-10-24 08:29:27

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

我找到了我同事的一个剧本。

我想要的只是来自美国和加拿大境内Google地图的自动填充功能。我看到我无法在componentRestrictions中添加多个国家/地区,但是,如果您知道一个非常有用的解决方法。

我在这里的剧本是:

<!DOCTYPE html>
<html>
  <head>
    <title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
    <style>
      body {
        font-family: sans-serif;
        font-size: 14px;
      }
      #map_canvas {
        height: 400px;
        width: 600px;
        margin-top: 0.6em;
      }
      input {
        border: 1px solid  rgba(0, 0, 0, 0.5);
      }
      input.notfound {
        border: 2px solid  rgba(255, 0, 0, 0.4);
      }
    </style>
    <script>
      function initialize() {
        var input = document.getElementById('searchTextField');
        var options = {
            types: ['(cities)'],
            componentRestrictions: {country: 'us'}
        };

        var autocomplete = new google.maps.places.Autocomplete(input, options);

        // Sets a listener on a radio button to change the filter type on Places
        // Autocomplete.
        function setupClickListener(id, types) {
          var radioButton = document.getElementById(id);
          google.maps.event.addDomListener(radioButton, 'click', function() {
            autocomplete.setTypes(types);
          });
        }

        setupClickListener('changetype-all', []);
        setupClickListener('changetype-establishment', ['establishment']);
        setupClickListener('changetype-geocode', ['geocode']);
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div>
      <input id="searchTextField" type="text" size="50">
      <input type="radio" name="type" id="changetype-all" checked="checked">
      <label for="changetype-all">All</label>

      <input type="radio" name="type" id="changetype-establishment">
      <label for="changetype-establishment">Establishments</label>

      <input type="radio" name="type" id="changetype-geocode">
      <label for="changetype-geocode">Geocodes</lable>
    </div>
  </body>
</html>

这个脚本有效!但是,问题是,我不想要单选按钮,当我删除它们和相关脚本时,它会完全停止工作!

我只是希望自动完成而不需要其他任何东西。 同样,这个脚本不是我写的。

编辑:

我的问题与https://gis.stackexchange.com/questions/31916/i-am-trying-to-put-autocomplete-of-google-api-v3-for-places-but-giving-some-err有关 谷歌给了我......虽然那个小伙子正在苦苦挣扎:(

4 个答案:

答案 0 :(得分:2)

2017年1月,Maps JavaScript API版本3.27中引入了多国家过滤器:

  

现在,您可以将自动填充预测限制为仅覆盖多个国家/地区。您可以通过在AutocompleteOptions的componentRestrictions字段中指定最多5个国家/地区来执行此操作。

https://developers.google.com/maps/documentation/javascript/releases

https://developers.google.com/maps/documentation/javascript/reference#ComponentRestrictions

看一下这个例子:http://jsbin.com/ramagi/edit?html,output

    var G = google.maps;
    var chicago = new G.LatLng(41.850033, -87.6500523);
    var infowindow = new G.InfoWindow();
    var marker;

      function initialize() {
      var mapOptions = {
          center: chicago,
          zoom: 5,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);
        marker = new G.Marker({map:map});

        var input = document.getElementById('searchTextField');
        var options = {
            types: ['(cities)'],
            componentRestrictions: {country: ['us', 'ca']}
        };

        var autocomplete = new google.maps.places.Autocomplete(input, options);

        google.maps.event.addListener(autocomplete, 'place_changed', function() {
          infowindow.close();
          var place = autocomplete.getPlace();
          if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
          } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);  // Why 17? Because it looks good.
          }

          var image = new google.maps.MarkerImage(
              place.icon,
              new google.maps.Size(71, 71),
              new google.maps.Point(0, 0),
              new google.maps.Point(17, 34),
              new google.maps.Size(35, 35));
          marker.setIcon(image);
          marker.setPosition(place.geometry.location);

          var address = '';
          if (place.address_components) {
            address = [(place.address_components[0] &&
                        place.address_components[0].short_name || ''),
                       (place.address_components[1] &&
                        place.address_components[1].short_name || ''),
                       (place.address_components[2] &&
                        place.address_components[2].short_name || '')
                      ].join(' ');
          }

          infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
          infowindow.open(map, marker);
        });
        }

      google.maps.event.addDomListener(window, 'load', initialize);
body {
        font-family: sans-serif;
        font-size: 14px;
      }
      #map_canvas {
        height: 400px;
        width: 600px;
        margin-top: 0.6em;
      }
      input {
        border: 1px solid  rgba(0, 0, 0, 0.5);
      }
      input.notfound {
        border: 2px solid  rgba(255, 0, 0, 0.4);
      }
<div>
      <input id="searchTextField" type="text" size="50">
      <div id="map_canvas"></div>
</div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=places"></script>

答案 1 :(得分:1)

所有我必须做的只是删除线条,一个接一个......我注意到,我删除了额外的}直到Chrome控制台告诉我! 简单的解决方案。

<!DOCTYPE html>
<html>
  <head>
    <title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
    <style>
      body {
        font-family: sans-serif;
        font-size: 14px;
      }
      #map_canvas {
        height: 400px;
        width: 600px;
        margin-top: 0.6em;
      }
      input {
        border: 1px solid  rgba(0, 0, 0, 0.5);
      }
      input.notfound {
        border: 2px solid  rgba(255, 0, 0, 0.4);
      }
    </style>
    <script>
      function initialize() {
        var input = document.getElementById('searchTextField');
        var options = {
            componentRestrictions: {country: 'us'}
        };
        var autocomplete = new google.maps.places.Autocomplete(input, options);
}

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
<form method = "POST">
    <div>
      <input id="searchTextField" type="text" size="50" name="location">
    </div>
<input type = "submit" />
  </body>
</html>

答案 2 :(得分:0)

如果你不想要任何单选按钮,只需用以下内容替换它们:

<div name="type" id="changetype-all" checked="checked"></div>
<label for="changetype-all">All</label>

<div name="type" id="changetype-establishment"></div>
<label for="changetype-establishment">Establishments</label>

<div name="type" id="changetype-geocode"></div>
<label for="changetype-geocode">Geocodes</lable>

答案 3 :(得分:0)

复制+粘贴服务(Live demo here。):

<!DOCTYPE html>
<html>
  <head>
    <title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
    <style>
      body {
        font-family: sans-serif;
        font-size: 14px;
      }
      #map_canvas {
        height: 400px;
        width: 600px;
        margin-top: 0.6em;
      }
      input {
        border: 1px solid  rgba(0, 0, 0, 0.5);
      }
      input.notfound {
        border: 2px solid  rgba(255, 0, 0, 0.4);
      }
    </style>
    <script>
    var G = google.maps;
    var chicago = new G.LatLng(41.850033, -87.6500523);
    var infowindow = new G.InfoWindow();
    var marker;

      function initialize() {
      var mapOptions = {
          center: chicago,
          zoom: 5,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);
        marker = new G.Marker({map:map});

        var input = document.getElementById('searchTextField');
        var options = {
            types: ['(cities)'],
            componentRestrictions: {country: 'us'}
        };

        var autocomplete = new google.maps.places.Autocomplete(input, options);

        google.maps.event.addListener(autocomplete, 'place_changed', function() {
          infowindow.close();
          var place = autocomplete.getPlace();
          if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
          } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);  // Why 17? Because it looks good.
          }

          var image = new google.maps.MarkerImage(
              place.icon,
              new google.maps.Size(71, 71),
              new google.maps.Point(0, 0),
              new google.maps.Point(17, 34),
              new google.maps.Size(35, 35));
          marker.setIcon(image);
          marker.setPosition(place.geometry.location);

          var address = '';
          if (place.address_components) {
            address = [(place.address_components[0] &&
                        place.address_components[0].short_name || ''),
                       (place.address_components[1] &&
                        place.address_components[1].short_name || ''),
                       (place.address_components[2] &&
                        place.address_components[2].short_name || '')
                      ].join(' ');
          }

          infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
          infowindow.open(map, marker);
        });
        }

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div>
      <input id="searchTextField" type="text" size="50">
      <div id="map_canvas"></div>
    </div>
  </body>
</html>