【问题标题】:Can I Limit Google Maps API Autocomplete results to Ireland and Northern Ireland我可以将 Google Maps API 自动填充结果限制在爱尔兰和北爱尔兰吗
【发布时间】:2017-10-28 10:43:28
【问题描述】:

目前,我可以将结果限制在爱尔兰和英国 (GB),但我需要将结果限制在爱尔兰岛(爱尔兰和北爱尔兰),但由于北爱尔兰是英国的一部分,因此我正在为整个英国使用国家代码ie & gb

但我如何将其限制在 Country-Ireland 和 Region-Northern Ireland?

提前感谢您的帮助

var countryCodes = ['ie', 'gb'];

        function initMap() {
            var autoInputs = [];

            // Destroy all existing inputs and reload them.
            function autoAllInputs() {
                autoInputs = [];
                $.each($(".js-location-input"), function( i, input ) {
                    var ainput = new google.maps.places.Autocomplete(input);
                    ainput.setComponentRestrictions({'country': countryCodes});
                    autoInputs.push(ainput);
                });
            }

            var $template = $(".js-repeat-template");
            var $container = $(".js-repeated-items");

            $(".js-add-repeatable").on("click", function(e) {
                e.preventDefault();
                $container.append($template.html());
                autoAllInputs();
            });

            autoAllInputs();

【问题讨论】:

  • 请更新问题以包含您当前的 api 调用
  • 刚刚添加的代码。谢谢

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


【解决方案1】:

目前无法在自动完成的地方添加管理区域过滤器。您的用例的唯一解决方法是定义一个包含爱尔兰和北爱尔兰的边界,并将这些边界与strictBounds 参数一起设置为在autocomplete options 中等于true。

概念证明

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: 53.435719, lng: -7.77832},
    zoom: 6,
    scaleControl: true
  });
  
  var irelandBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(50.999929,-10.854492),
    new google.maps.LatLng(55.354135,-5.339355));
    
  map.fitBounds(irelandBounds);  
  
  var boundsPoly = new google.maps.Polyline({
    map: map,
    path: [
      {lat: irelandBounds.getSouthWest().lat(), lng: irelandBounds.getSouthWest().lng()},
      {lat: irelandBounds.getSouthWest().lat(), lng: irelandBounds.getNorthEast().lng()},
      {lat: irelandBounds.getNorthEast().lat(), lng: irelandBounds.getNorthEast().lng()},
      {lat: irelandBounds.getNorthEast().lat(), lng: irelandBounds.getSouthWest().lng()},
      {lat: irelandBounds.getSouthWest().lat(), lng: irelandBounds.getSouthWest().lng()}
    ],
    strokeColor: "#FF0000"
  });

  
  var input = /** @type {!HTMLInputElement} */(
      document.getElementById('pac-input'));

  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
  
  var autocomplete = new google.maps.places.Autocomplete(input, {
    bounds: irelandBounds,
    strictBounds: true
  });

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map,
    anchorPoint: new google.maps.Point(0, -29)
  });

  autocomplete.addListener('place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      window.alert("Autocomplete's returned place contains no geometry");
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);  // Why 17? Because it looks good.
    }
    marker.setIcon(/** @type {google.maps.Icon} */({
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(35, 35)
    }));
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    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);
  });
}
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
.controls {
  margin-top: 10px;
  border: 1px solid transparent;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  height: 32px;
  outline: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#pac-input {
  background-color: #fff;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  margin-left: 12px;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 300px;
}

#pac-input:focus {
  border-color: #4d90fe;
}

.pac-container {
  font-family: Roboto;
}
<input id="pac-input" class="controls" type="text"
        placeholder="Enter a location">
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=places&callback=initMap"
        async defer></script>

要为不显示地图的自动完成文本框添加限制,您只需要这样做:

var input = document.getElementById('pac-input');

var irelandBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(50.999929,-10.854492),
  new google.maps.LatLng(55.354135,-5.339355));

var autocomplete = new google.maps.places.Autocomplete(input, {
  bounds: irelandBounds,
  strictBounds: true
});

<input id="pac-input" type="text" placeholder="Enter a location">

我希望这会有所帮助!

【讨论】:

  • 完美的xomena!
  • 优秀的答案。我只需要限制没有地图的文本框的自动完成,所以我编辑了你的答案以提取代码,因为它可能是其他人正在寻找的东西......希望没关系:)
【解决方案2】:

12. Maps API Standard Pricing Plan 12.1 免费配额。服务的某些部分是免费提供给您的,但不超过 Maps API 文档中描述的交易限额。

我认为不受国家限制

每天 25000 次下载: - 谷歌地图 JavaScript API - 谷歌静态地图 API - 谷歌街景图像 API

每天 2500 个请求: - 谷歌地图路线 API - 谷歌地图距离矩阵 API 4 - 谷歌地图海拔 API - 谷歌地图地理编码 API - 谷歌地图地理定位 API - 谷歌地图道路 API - 谷歌地图时区 API

contact Sales如果需要更多..

【讨论】:

  • 我的自动完成功能工作正常,但是当我添加英国时,北爱尔兰给我带来了问题,因此北爱尔兰也包含在结果中 - 我也得到了整个英国的结果
  • 大不列颠及北爱尔兰联合王国 = GB;爱尔兰 = IE ......但需要北爱尔兰 + 爱尔兰 .. 好的
  • &bounds=X1,Y1|X2,Y2(其中 XY "northeast" : {"lat" : 1.0000, "lng": 0.0000}, "southwest" : { "lat" : 0.0000, " lng" : 1.0000 }) 和 xomena 的回答(TY 引起注意)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多