get city from geocoder results?

后端 未结 13 1847
我寻月下人不归
我寻月下人不归 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);
       }            
    }); 
    
    0 讨论(0)
  • 2020-12-02 14:43

    This worked for me:

    const localityObject = body.results[0].address_components.filter((obj) => {
      return obj.types.includes('locality');
    })[0];
    const city = localityObject.long_name;
    

    or in one go:

    const city = body.results[0].address_components.filter((obj) => {
      return obj.types.includes('locality');
    )[0].long_name;
    

    I'm doing this in Node, so this is okay. If you need to support IE you need to use a polyfill for Array.prototype.includes or find another way of doing it.

    0 讨论(0)
  • 2020-12-02 14:44

    I am assuming you want to get the city and the state / province:

    var map_center = map.getCenter();
    reverseGeocode(map_center);
    
    
    function reverseGeocode(latlng){
      geocoder.geocode({'latLng': latlng}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
                var level_1;
                var level_2;
                for (var x = 0, length_1 = results.length; x < length_1; x++){
                  for (var y = 0, length_2 = results[x].address_components.length; y < length_2; y++){
                      var type = results[x].address_components[y].types[0];
                        if ( type === "administrative_area_level_1") {
                          level_1 = results[x].address_components[y].long_name;
                          if (level_2) break;
                        } else if (type === "locality"){
                          level_2 = results[x].address_components[y].long_name;
                          if (level_1) break;
                        }
                    }
                }
                updateAddress(level_2, level_1);
           } 
      });
    }
    
    function updateAddress(city, prov){
       // do what you want with the address here
    }
    

    Don't try to return the results as you will find that they are undefined - a result of an asynchronous service. You must call a function, such as updateAddress();

    0 讨论(0)
  • 2020-12-02 14:48
                //if (arrAddress[ac].types[0] == "street_number") { alert(arrAddress[ac].long_name) } // SOKAK NO
                //if (arrAddress[ac].types[0] == "route") { alert(arrAddress[ac].short_name); } // CADDE
                //if (arrAddress[ac].types[0] == "locality") { alert(arrAddress[ac].long_name) } // İL
                //if (arrAddress[ac].types[0] == "administrative_area_level_1") { alert(arrAddress[ac].short_name) } // İL
                //if (arrAddress[ac].types[0] == "postal_code") { alert(arrAddress[ac].long_name); } // POSTA KODU
                //if (arrAddress[ac].types[0] == "neighborhood") { alert(arrAddress[ac].long_name); } // Mahalle
                //if (arrAddress[ac].types[0] == "sublocality") { alert(arrAddress[ac].long_name); } // İlçe
                //if (arrAddress[ac].types[0] == "country") { alert(arrAddress[ac].long_name); } // Ülke
    
    0 讨论(0)
  • 2020-12-02 14:58

    Got this working in the end using:

    var arrAddress = item.address_components;
    var itemRoute='';
    var itemLocality='';
    var itemCountry='';
    var itemPc='';
    var itemSnumber='';
    
    // iterate through address_component array
    $.each(arrAddress, function (i, address_component) {
        console.log('address_component:'+i);
    
        if (address_component.types[0] == "route"){
            console.log(i+": route:"+address_component.long_name);
            itemRoute = address_component.long_name;
        }
    
        if (address_component.types[0] == "locality"){
            console.log("town:"+address_component.long_name);
            itemLocality = address_component.long_name;
        }
    
        if (address_component.types[0] == "country"){ 
            console.log("country:"+address_component.long_name); 
            itemCountry = address_component.long_name;
        }
    
        if (address_component.types[0] == "postal_code_prefix"){ 
            console.log("pc:"+address_component.long_name);  
            itemPc = address_component.long_name;
        }
    
        if (address_component.types[0] == "street_number"){ 
            console.log("street_number:"+address_component.long_name);  
            itemSnumber = address_component.long_name;
        }
        //return false; // break the loop   
    });
    
    0 讨论(0)
  • 2020-12-02 14:58

    Here's some code you can use with the lodash js library: (just replace the $scope.x with your own variable name to store the value)

        _.findKey(vObj.address_components, function(component) {
    
                if (component.types[0] == 'street_number') {
                    $scope.eventDetail.location.address = component.short_name
                }
    
                if (component.types[0] == 'route') {
                    $scope.eventDetail.location.address = $scope.eventDetail.location.address + " " + component.short_name;
                }
    
                if (component.types[0] == 'locality') {
                    $scope.eventDetail.location.city = component.long_name;
                }
    
                if (component.types[0] == 'neighborhood') {
                    $scope.eventDetail.location.neighborhood = component.long_name;
                }
    
            });
    
    0 讨论(0)
提交回复
热议问题