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

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

10 Comments

sorry to be so super dumb but, will that be under initialize() ?
I need to check the goolge autocompleate syntax.
there you go! Just replace you JS
replace my JS with? sorry, I know, I am testing your patience but, which JS is it?
I replaced it with maps.google.com/maps/api/… and it console tells me : Uncaught Error: Property input is not specified.
|
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.

Comments

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

2 Comments

Dang! How do I put it? in my initialize() ?
I have partially copied my code from that reference link only. If you see closely, I have not even removed the default id :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.