本文介绍了我可以将Google Maps API自动填充结果限制为爱尔兰和北爱尔兰吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently I can limit the results to Ireland and UK (GB) but I need to Limit the results to just the Island of Ireland (Ireland & Northern Ireland) but as Northern Ireland is part of the UK I am getting results for the entire UK using country codes ie & gb

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

But how would I limit this to the Country-Ireland and Region-Northern Ireland?

非常感谢您的帮助

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

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();

推荐答案

当前无法在自动完成的地方添加管理区域过滤器.您的用例唯一的解决方法是定义一个包括爱尔兰和北爱尔兰的界限,并在自动填充选项.

Currently there is no way to add admin area filters in places autocomplete. The only workaround for your use case is defining a bounds that includes Ireland and Northern Ireland and set these bounds in conjunction with a strictBounds parameter equals to true in autocomplete options.

概念证明

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>

我希望这会有所帮助!

I hope this helps!

这篇关于我可以将Google Maps API自动填充结果限制为爱尔兰和北爱尔兰吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 11:27