get city from geocoder results?

后端 未结 13 1950
我寻月下人不归
我寻月下人不归 2020-12-02 14:07

Having problems getting the different arrays content from geocoder results.

item.formatted_address works but not item.address_components.locality?

ge         


        
13条回答
  •  长情又很酷
    2020-12-02 15:08

    tried a couple of different requests:

    MK107BX

    Cleveland Park Crescent, UK

    like you say, array size returned is inconsistent but the Town for both results appears to be in the address_component item with type of [ "locality", "political" ]. Perhaps you could use that as an indicator?

    EDIT: get the locality object using jQuery, add this to your response function:

    var arrAddress = item.results[0].address_components;
    // iterate through address_component array
    $.each(arrAddress, function (i, address_component) {
        if (address_component.types[0] == "locality") // locality type
            console.log(address_component.long_name); // here's your town name
            return false; // break the loop
        });
    

提交回复
热议问题