Google API Address Components

前端 未结 3 1612
借酒劲吻你
借酒劲吻你 2020-12-12 07:02

I am attempting to obtain the address components using the Google Maps API however am unable to properly parse results. My code is as follows:

    // Ajax C         


        
相关标签:
3条回答
  • 2020-12-12 07:11

    I'm assuming what you are doing is a reverse geocode.

    The object returned looks something like this (from the example):

    {
      "address_components": [
        {
          "long_name": "Grand St/Bedford Av",
          "short_name": "Grand St/Bedford Av",
          "types": [
            "bus_station",
            "transit_station",
            "establishment"
          ]
        },
        {
          "long_name": "Williamsburg",
          "short_name": "Williamsburg",
          "types": [
            "neighborhood",
            "political"
          ]
        },
        {
          "long_name": "Brooklyn",
          "short_name": "Brooklyn",
          "types": [
            "sublocality_level_1",
            "sublocality",
            "political"
          ]
        },
    ...
    

    There is no direct way to access a result's street, house number etc. because each part of the address can be several types at the same time. You would have to iterate over the address_components and filter according to what you need.

    Here's another Stackoverflow thread covering this topic: get city from geocoder results?

    0 讨论(0)
  • 2020-12-12 07:23

    a little late but this is that i have

    calle === street

    nro === street_number

    extraerCalle(resultado: IResult): string | undefined {
        const route = resultado.address_components.find(ac => !!ac.types.find(type => type === 'route'));
        return route && route.long_name;
      }
    
      extraerNro(resultado: IResult): string | undefined {
        const streetNumber = resultado.address_components.find(ac => !!ac.types.find(type => type === 'street_number'));
        return streetNumber && streetNumber.long_name;
      }
    
      extraerCalleNro(resultado: IResult): string {
        const calle = this.extraerCalle(resultado);
        const nro = this.extraerNro(resultado);
        return (calle || '') + (calle && nro ? ` ${nro}` : '');
      }
    
    0 讨论(0)
  • 2020-12-12 07:30

    I wrote a function.

    /**
    *   geocodeResponse is an object full of address data.  
    *   This function will "fish" for the right value
    *   
    *   example: type = 'postal_code' => 
    *   geocodeResponse.address_components[5].types[1] = 'postal_code'
    *   geocodeResponse.address_components[5].long_name = '1000'
    * 
    *   type = 'route' => 
    *   geocodeResponse.address_components[1].types[1] = 'route'
    *   geocodeResponse.address_components[1].long_name = 'Wetstraat'
    */
    function addresComponent(type, geocodeResponse, shortName) {
      for(var i=0; i < geocodeResponse.address_components.length; i++) {
        for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) {
          if (geocodeResponse.address_components[i].types[j] == type) {
            if (shortName) {
              return geocodeResponse.address_components[i].short_name;
            }
            else {
              return geocodeResponse.address_components[i].long_name;
            }
          }
        }
      }
      return '';
    }
    

    example of how to use:

    ...
    myGeocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK && results[1]) {
        var country = addresComponent('country', results[1], true);
        var postal_code = addresComponent('postal_code', results[1], true);
      }
    });
    ...
    
    0 讨论(0)
提交回复
热议问题