how to use place_changed event of placekicker of google api?

343 views Asked by At

I have almost done but i am facing one problem here is my problem :-

I have successfully implement the placekicker with uk country only. Here is my code:-
Input location here:<input id="searchTextField" type="text" size="50">
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script>
var pac_input = document.getElementById('searchTextField');
$(function() {
  var options = {
    componentRestrictions: {
      country: 'uk' //PUT YOUR COUNTRY CODE HERE!!
    }
  };
  var autocomplete = new google.maps.places.Autocomplete(pac_input, options);
});

When i run the above code it will displaying me only uk result i.e is right but when i add some more functionality in above code then it displaying whole countries record. The added code is here:-

<script type="text/javascript">
    google.maps.event.addDomListener(window, 'load', function () {
        var places = new google.maps.places.Autocomplete(document.getElementById('searchTextField'));
        google.maps.event.addListener(places, 'place_changed', function () {
            var place = places.getPlace();
            var address = place.formatted_address;
            var latitude = place.geometry.location.lat();
            var longitude = place.geometry.location.lng();
            var mesg = "Address: " + address;
            mesg += "\nLatitude: " + latitude;
            mesg += "\nLongitude: " + longitude;
            alert(mesg);
        });
    });
</script>

when i add this code it will give latitude and longitude of every place which i have select from placepicker now problem is that it will give whole country data but when i remove this code it gives me uk result. please help me. What i am doing wrong. Thanks in advance :)

1

There are 1 answers

0
Sharad Kale On BEST ANSWER

Try passing options in Autocomplete function

var options = {
    componentRestrictions: {
      country: 'uk' //PUT YOUR COUNTRY CODE HERE!!
    }
  };
    google.maps.event.addDomListener(window, 'load', function () {
        var places = new google.maps.places.Autocomplete(document.getElementById('searchTextField'), options );  //// options added
        google.maps.event.addListener(places, 'place_changed', function () {
            var place = places.getPlace();
            var address = place.formatted_address;
            var latitude = place.geometry.location.lat();
            var longitude = place.geometry.location.lng();
            var mesg = "Address: " + address;
            mesg += "\nLatitude: " + latitude;
            mesg += "\nLongitude: " + longitude;
            alert(mesg);
        });
    });