ItSolutionStuff.com

Google Maps Autocomplete Search Only One Country

By Hardik Savani • July 4, 2023
Javascript jQuery

I want to show you how to limit google autocomplete results to city and country only in google map. we can Restrict AutoComplete results to specific country in google map. we can allow only specific country to autocomplete search in google maps.

We are using google map autocomplete for choosing address with latitude and longitude. but if you require to search only one country like in, us, uk etc then you can do it using setComponentRestrictions option in google map.

In bellow example you can see how restrict other country for autocomplete search with google map api. so let's see bellow example, you can search only india country.

You can do it like this way, i added search only for india and us now:

autocomplete.setComponentRestrictions({'country': ['in', 'us']});

You can see bellow full example:

Example:

<!DOCTYPE html>

<html>

<head>

<title>Autocomplete Address Search Box using Google Maps Javascript API</title>

<style type="text/css">

.mapControls {

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

}

#searchMapInput {

background-color: #fff;

font-family: Roboto;

font-size: 15px;

font-weight: 300;

margin-left: 12px;

padding: 0 11px 0 13px;

text-overflow: ellipsis;

width: 50%;

}

#searchMapInput:focus {

border-color: #4d90fe;

}

</style>

</head>

<body>

<h1>Autocomplete Address Search Box using Google Maps Javascript API</h1>

<input id="searchMapInput" class="mapControls" type="text" placeholder="Enter a location">

<ul id="geoData">

<li>Full Address: <span id="location-snap"></span></li>

<li>Latitude: <span id="lat-span"></span></li>

<li>Longitude: <span id="lon-span"></span></li>

</ul>

<script>

function initMap() {

var input = document.getElementById('searchMapInput');

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

autocomplete.setComponentRestrictions({'country': ['in']});

autocomplete.addListener('place_changed', function() {

var place = autocomplete.getPlace();

document.getElementById('location-snap').innerHTML = place.formatted_address;

document.getElementById('lat-span').innerHTML = place.geometry.location.lat();

document.getElementById('lon-span').innerHTML = place.geometry.location.lng();

});

}

</script>

<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMap" async defer></script>

</body>

</html>

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to use Google ReCaptcha V2 in Laravel 10?

Read Now →

Laravel 10 Google Recaptcha V3 Example Tutorial

Read Now →

Laravel 10 Google Charts Tutorial Example

Read Now →

Angular 14 Login with Google Account Example

Read Now →

Laravel Google ReCaptcha V3 Tutorial Example

Read Now →

Google Maps API Google Map with Draggable Marker Example

Read Now →

Google Maps API - Autocomplete Address Search Box with Map Example

Read Now →

Autocomplete Places Search Box using Google Maps Javascript API

Read Now →

How to use Google ReCaptcha in Laravel?

Read Now →

Laravel Multiple Markers in Google Map using Gmaps.js

Read Now →

How to Integrate Google Map using Gmaps JS?

Read Now →

How to Add Multiple Markers in Google Map using JQuery?

Read Now →

How to use Google Maps API with JQuery PHP?

Read Now →