How to limit google maps auto complete to only one country?
I have tried everything so my search input only gives autocomplete options from only Dominican Republic but I have not been able to achieve it, anyone here knows how to do this?
<input
type="text"
id="searchInput"
class="form-control"
placeholder="Search for a location..."
>
let autocomplete;
const input = document.getElementById('searchInput');
autocomplete = new google.maps.places.Autocomplete(input, {
types: ['(cities)', 'geocode'], // Allow both city and address searches
componentRestrictions: { country: 'do' }
});
// Handle place selection
autocomplete.addListener('place_changed', function() {
const place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
You can use this method; it only suggests countries in the Dominican Republic and works well.
If you are using javascript then below is the code for that
import React, { useEffect, useRef } from 'react';
export function App() {
const inputRef = useRef(null);
const autocompleteRef = useRef(null);
useEffect(() => {
if (window.google) {
// Initialize the Google Maps Places Autocomplete
autocompleteRef.current = new window.google.maps.places.Autocomplete(
inputRef.current,
{
types: ['(cities)'], // Only restrict to cities
componentRestrictions: { country: 'do' }, // Limit to Dominican Republic
}
);
// Handle the place selection
autocompleteRef.current.addListener('place_changed', () => {
const place = autocompleteRef.current.getPlace();
if (place.geometry) {
console.log('Selected place:', place);
// You can handle the selected place here
} else {
console.log('No place found.');
}
});
}
}, []);
return (
<div className='App'>
<h1>Google Maps Autocomplete</h1>
<input
ref={inputRef}
type='text'
className='form-control'
placeholder='Search for a location in Dominican Republic...'
/>
</div>
);
}
and if you want to use html then this works fine.
You just need to add YOUR_GOOGLE_MAPS_API_KEY
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Maps Autocomplete</title>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_GOOGLE_MAPS_API_KEY&libraries=places&callback=initAutocomplete" async defer></script>
<style>
.container {
max-width: 400px;
margin: 0 auto;
padding-top: 50px;
}
input {
width: 100%;
padding: 8px;
font-size: 16px;
}
</style>
</head>
<body>
<div class="container">
<h1>Google Maps Autocomplete</h1>
<input id="autocomplete" type="text" class="form-control" placeholder="Search for a location in Dominican Republic...">
</div>
<script>
let autocomplete;
// Initialize the Google Maps Places Autocomplete
function initAutocomplete() {
const input = document.getElementById('autocomplete');
autocomplete = new google.maps.places.Autocomplete(input, {
types: ['(cities)'], // Only restrict to cities
componentRestrictions: { country: 'do' }, // Limit to Dominican Republic
});
// Handle the place selection
autocomplete.addListener('place_changed', function() {
const place = autocomplete.getPlace();
if (place.geometry) {
console.log('Selected place:', place);
// Handle the selected place here
} else {
console.log('No place found.');
}
});
}
</script>
</body>
</html>