Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google places autocomplete restrict to particular area

My requirement is to get google places autocomplete suggestion only for Bangalore places, but I am not getting places only for Bangalore or within mention latitude longitude.

I want to retireve only below image places in autocomplete textfield.

enter image description here

can someone plz suggest how to achieve the same and where I am going wrong.

Javascript:

<script type="text/javascript">


   function initialize1() {

    var southWest = new google.maps.LatLng( 12.97232, 77.59480 );
    var northEast = new google.maps.LatLng( 12.89201, 77.58905 );
    var bangaloreBounds = new google.maps.LatLngBounds( southWest, northEast );

    var options = {
        bounds: bangaloreBounds,
        types: ['(cities)'],
        componentRestrictions: {country: 'in'}
    };

     var input = document.getElementById('searchTextFieldTo');
     var autocomplete = new google.maps.places.Autocomplete(input, options);
   }
   google.maps.event.addDomListener(window, 'load', initialize1);
</script>

TextField:

<input type="text" id="searchTextFieldTo" class="ui-timepicker-hour" style="width:350px;text-align:left;font-style:italic;" placeholder="Enter To location" autocomplete="on" />
like image 463
Jayesh Avatar asked Dec 11 '12 17:12

Jayesh


1 Answers

Google Provides two ways to achieve this. If you are not satisfied because in countries like India it do not work well, because states and provisions here do not have rectangular or structure boundaries.

1.LatLngBounds (LatLng southwest, LatLng northeast): Where you can give latitude and longitude to form an rectangle.

2. Location (Lat,Lng) & Radius: Where you can give latitude and longitude to form a circle.

But the problem with these approaches they do not provide expected results if you are from countries like India, where states and provisions are not in structured shapes (Rectangular) as in USA.

If you are facing same issue than there is an hack. With jQuery/Jacascript, you can attach functions which will consistently maintain city name in text input which is bounded with Autocomplete object of Places API.

Here it is:

<script>
    $(document).ready(function(){
        $("#locality").val(your-city-name)  //your-city-name will have city name and some space to seperate it from actual user-input for example: “Bengaluru | ”
        });

    $("#locality").keydown(function(event) { //locality is text-input box whixh i supplied while creating Autocomplete object
    var localeKeyword = “your-city-name”
    var localeKeywordLen = localeKeyword.length;
    var keyword = $("#locality").val();
    var keywordLen = keyword.length;

    if(keywordLen == localeKeywordLen) {
        var e = event || window.event;  
        var key = e.keyCode || e.which; 

        if(key == Number(46) || key == Number(8) || key == Number(37)){
            e.preventDefault(); 
            }//Here I am restricting user to delete city name (Restricting use of delete/backspace/left arrow) if length == city-name provided

        if(keyword != localeKeyword) {
            $("#locality").val(localeKeyword);
            }//If input-text does not contain city-name put it there
        }

    if(!(keyword.includes(localeKeyword))) {
        $("#locality").val(localeKeyword);
        }//If keyworf not includes city name put it there
    });
</script>

(Image:) Before This Hack

enter image description here

(Image:) After This hack

enter image description here

like image 123
Ravi Upadhyay Avatar answered Oct 19 '22 01:10

Ravi Upadhyay