How to get country from google maps api?

后端 未结 1 1910
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 05:48

I use this script:

var place = autocomplete.getPlace();
latitude = place.geometry.location.lat();
longitude = place.geometry.location.lng();

street_number =         


        
相关标签:
1条回答
  • 2021-01-14 06:32

    You shouldn't rely on index of element in place.address_components array. Instead you have to analyze the "types" field to find the corresponding address component for the country.

    It can be done with Array.prototype.filter() and Array.prototype.includes() functions.

    var filtered_array = place.address_components.filter(function(address_component){
        return address_component.types.includes("country");
    }); 
    var country = filtered_array.length ? filtered_array[0].long_name: "";
    
    0 讨论(0)
提交回复
热议问题