0

I need from Google Place Autocomplete these values:

alert(
  place.name +                       // Works
  place.geometry.location.lat() +    // Works
  place.country +  // Not works - what is correct value to get country value?
  place.state      // Not works - what is correct value to get state value?
  );

It works for place.name and place.geometry, but it not works for place.country and place.state.

How to get also the other two values? Thank you for any advice.

Whole function:

function initializeGoogleMaps() {
    var input = document.getElementById("googleMapsSearch");
    var autocomplete = new google.maps.places.Autocomplete(input);
     google.maps.event.addListener(
        autocomplete,
        "place_changed",
        function() {
        var place = autocomplete.getPlace();
        alert(
          place.name +                       // Works
          place.geometry.location.lat() +    // Works
          place.country +  // Not works - what is correct value to get country value?
          place.state      // Not works - what is correct value to get state value?
        );
        document.getElementById("city").value = place.name;   // Works
        document.getElementById("cityLat").value = place.geometry.location.lat();   // Works
        document.getElementById("country").value = place.country;   // Not works
        document.getElementById("state").value = place.state;   // Not works
     });
}
cristtiano
  • 17
  • 3
  • 1
    https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform – MrUpsidown May 23 '20 at 16:09
  • @MrUpsidown thats how I got country and state, but example doesnt show how to get also lat and lng – cristtiano May 23 '20 at 17:15
  • 1
    `place.geometry.location.lat()` - That's in your code. What have you tried to debug? Anyway you should provide a [mcve] that includes all and only (!) the necessary code to reproduce the issue and a clear description of what works and what doesn't. – MrUpsidown May 23 '20 at 17:36
  • @MrUpsidown Sorry, I updated question with clear description and code. – cristtiano May 23 '20 at 20:04
  • 1) this is not a [mcve] 2) instead of using `alert` on a property name you aren't sure, use `console.log()` and log the `place` variable which contains **everything** you need 3) the example I linked to definitely doesn't use `place.country` which doesn't exist 4) learn to debug your code – MrUpsidown May 23 '20 at 23:33
  • Also by reading the official docs you can see that `autocomplete.getPlace()` returns a [PlaceResult](https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceResult) object which is fully documented with **all** possible properties. – MrUpsidown May 23 '20 at 23:40
  • 1
    As I am learning this is where I needed to pointed to: use console.log() and log the place variable which contains everything you need. Thank you. – cristtiano May 24 '20 at 02:04

1 Answers1

0

You can get the state and the country under the address_components array returned by the getDetails() method. Since the address components is being returned as an array, you can parse the results like this:

var country;
var state;
var components = place.address_components;

   for(i=0;i<components.length;i++){
       if(place.address_components[i].types[0].toString() === 'administrative_area_level_1'){
          state = place.address_components[i].long_name;
       }else if(place.address_components[i].types[0].toString() === 'country'){
           country = place.address_components[i].long_name;
           } 
     }

Note: administrative_area_level_1 usually signifies to the state and varies depending on the political heirarchy of a certain country.

Here's a complete sample you can refer to. You just need to replace "YOUR_API_KEY" with your own API key.

For more information regarding Place details results, please checkout this link: https://developers.google.com/maps/documentation/javascript/places#place_details_results

Hope this helps!

smga08
  • 614
  • 5
  • 12