Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google maps autocomplete bounds not working

I want to use autocomplete bounds on Google maps service. But not working.

//----autocomplete start
var input = (document.getElementById('searchPlace'));
var defaultBounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(40.518, 29.215),
            new google.maps.LatLng(41.242, 30.370));

var options = {
            bounds:defaultBounds,
            componentRestrictions: { country: 'XX'},};

var autocomplete = new google.maps.places.Autocomplete(input, options);
//----autocomplete end

When I type the textbox, the addresses are coming in XX country. But I wanna restrict the search in a region of country. For example a city bounds. But other addresses are coming out of this bounds. Bounds are not considered.

like image 451
barteloma Avatar asked Sep 20 '13 06:09

barteloma


1 Answers

The problem is that LatLngBounds expects south-west and north-east corners. Also referred as bottom-left and top-right corners.

Instead of:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(40.518, 29.215),
    new google.maps.LatLng(41.242, 30.370));

It should be:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(41.242, 29.215),
    new google.maps.LatLng(40.518, 30.370));
like image 138
Diego Cerdan Puyol Avatar answered Sep 19 '22 23:09

Diego Cerdan Puyol