get city from geocoder results?

后端 未结 13 1914
我寻月下人不归
我寻月下人不归 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 14:43

    I think it is a real pain that google doesn't provide some sort of functionality to get these. Anyhow I think the best way of finding the right object is:

    geocoder.geocode({'address': request.term }, function(results, status){
    
       response($.map(results, function(item){
    
          var city = $.grep(item.address_components, function(x){
             return $.inArray('locality', x.types) != -1;
          })[0].short_name;
    
          alert(city);
       }            
    }); 
    

提交回复
热议问题