Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Limit Google Maps API Autocomplete results to Ireland and Northern Ireland

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

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

Thanks for your help in advance

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();
like image 541
Ian Butler Avatar asked Nov 29 '25 06:11

Ian Butler


1 Answers

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.

Proof of concept

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>

To add the limit for the Autocomplete textbox without map display, you only need this:

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

I hope this helps!

like image 130
xomena Avatar answered Dec 02 '25 04:12

xomena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!