0

I have a script here:

<!DOCTYPE html>
<html>
  <head>
    <title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
    <script>
      function initialize() {
        var input = document.getElementById('searchTextField');
        var options = {
            types: ['(cities)'],
            componentRestrictions: {country: 'us'}
        };

        var autocomplete = new google.maps.places.Autocomplete(input, options);
}
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div>
      <input id="searchTextField" type="text" size="50">
    </div>
  </body>
</html>

All I want to do is, get the JSON output from the autocomplete and extract long_name , short_name of administrative_area_level_1 and administrative_area_level_2 see the image below.

Screenshot of what I want

Keval Domadia
  • 4,768
  • 1
  • 37
  • 64

3 Answers3

1

Use the http://api.jquery.com/jQuery.parseJSON/ Like this:

var results = jQuery.parseJSON(google_json);

Then:

alert(results[0].types[0]);
alert(results[0].types[1]);

So with the google-places autocompleate you need to do this like in this link: https://developers.google.com/maps/documentation/javascript/places?hl=pl#places_autocomplete

var input = document.getElementById('searchTextField'); // input must be global
function initialize() {

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

    //var autocomplete = new google.maps.places.Autocomplete(input, options);
    var service = new google.maps.places.AutocompleteService();
    service.getQueryPredictions(input, callback);



}

function callback(predictions, status) {
    for (var i = 0, prediction; prediction = predictions[i]; i++) {
        alert(prediction.types[0]);
        alert(prediction.types[1]);
    }
}
google.maps.event.addDomListener(window, 'load', initialize);

Here you got live example: https://google-developers.appspot.com/maps/documentation/javascript/examples/places-queryprediction?hl=pl

Final Update

Use this script:

<script>

    function initialize() {
        var input = document.getElementById('searchTextField');
        var options = {
    types: ['(cities)'],
    componentRestrictions: {country: 'us'}
    };

    var autocomplete = new google.maps.places.Autocomplete(input, options);
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
       alert(this.types[0]);
       alert(this.types[1]);          
    });




    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>
Max Małecki
  • 1,700
  • 2
  • 13
  • 18
1

It was simple but, difficult to find:

Finally implemented GeoComplete JS using :

http://ubilabs.github.com/geocomplete/

Website is enough documented to assist with implementation.

Keval Domadia
  • 4,768
  • 1
  • 37
  • 64
0

If you want to process results after selection of place you have to bind event like below

google.maps.event.addListener(autocomplete, 'place_changed', your_function);

Code on following page will be very helpful to you https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete

Loken Makwana
  • 3,788
  • 2
  • 21
  • 14