获取地址_组件

问题描述 投票:0回答:8

我正在使用带地理编码功能的 google place API。我对地址组件类型有疑问。

我想获取有关用户以这种格式输入自动完成的地址的信息:
街道号码/街道/城市/省/国家。

如果用户自动完成“Street 12,SomeCity,SomeProvince,SomeCountry”,我想在警报中返回所有这些信息。但是当用户仅输入“someProvince,SomeCountry”时,我只想只有省和国家地址类型。

这是我的代码:

google.maps.event.addListener(autocomplete, 'place_changed', function () {
    var place = autocomplete.getPlace();
    alert('0: ' + place.address_components[0].long_name);
    alert('1: ' + place.address_components[1].long_name);
    alert('2: ' + place.address_components[2].long_name);
    alert('3: ' + place.address_components[3].long_name);
    alert('4: ' + place.address_components[4].long_name);
    alert('5: ' + place.address_components[5].long_name);
    alert('6: ' + place.address_components[6].long_name);
    alert('7: ' + place.address_components[7].long_name);
)};

问题是,当用户自动填写完整地址时,它会正确显示所有这些警报。但是,当仅自动完成部分信息(仅国家/地区)时,它将显示输入的国家/地区 7 次。

http://gmaps-samples-v3.googlecode.com/svn/trunk/places/autocomplete-addressform.html

我希望,当未给出街道和城市时,它会显示警报(“街道为空”等)。怎么办?

google-maps google-api google-places-api
8个回答
17
投票

地址类型和地址组件类型

使用类型并将它们映射到您的地址字段。请记住,城市、县、州等根据其上下文可能具有不同的含义。例如,

North Hollywood
会出现类型
neighborhood
,因为它位于洛杉矶('locality')。

function placeToAddress(place){
        var address = {};
        place.address_components.forEach(function(c) {
            switch(c.types[0]){
                case 'street_number':
                    address.StreetNumber = c;
                    break;
                case 'route':
                    address.StreetName = c;
                    break;
                case 'neighborhood': case 'locality':    // North Hollywood or Los Angeles?
                    address.City = c;
                    break;
                case 'administrative_area_level_1':     //  Note some countries don't have states
                    address.State = c;
                    break;
                case 'postal_code':
                    address.Zip = c;
                    break;
                case 'country':
                    address.Country = c;
                    break;
                /*
                *   . . . 
                */
            }
        });

        return address;
    }

14
投票

为项目制作了这个功能。它解析

  • 街道
  • 数字
  • 国家
  • 拉链
  • 城市

来自谷歌地理响应

function parseGoogleResponse(components) {
    _.each(components, function(component) {
      _.each(component.types, function(type) {
        if (type === 'route') {
          $("input[name=street]").val(component.long_name)
        }
        if (type === 'street_number') {
          $("input[name=nr]").val(component.long_name)
        }
        if (type === 'locality') {
          $("input[name=city]").val(component.long_name)
        }
        if (type === 'country') {
          $("input[name=country]").val(component.long_name)
        }
        if (type === 'postal_code') {
          $("input[name=zip]").val(component.long_name)
        }
      })
    })
  }

6
投票

根据演示,您需要检查每个返回的地址组件以查看是否已返回街道/城市:

google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    var components = place.address_components;
    var street = null;
    for (var i = 0, component; component = components[i]; i++) {
        console.log(component);
        if (component.types[0] == 'route') {
            street = component['long_name'];
        }
    }
    alert('Street: ' + street);
});

3
投票

为 AngularJS 制作了这个函数:

function getAddressComponentByPlace(place) {
    var components;

    components = {};

    angular.forEach(place.address_components, function(address_component) {
        angular.forEach(address_component.types, function(type) {
            components[type] = address_component.long_name;
        });
    });

    return components;
}

结果如下所示:

administrative_area_level_1: "Berlin"
country: "Deutschland"
locality: "Berlin"
political: "Deutschland"
postal_code: "10719"
route: "Kurfürstendamm"
street_number: "1"
sublocality: "Bezirk Charlottenburg-Wilmersdorf"
sublocality_level_1: "Bezirk Charlottenburg-Wilmersdorf"

ES6版本

  const parseGoogleAddressComponents = (addressComponents) => {
    let components = {};
    addressComponents.forEach((addressComponent) => {
      addressComponent.types.forEach((type) => {
        components[type] = addressComponent.long_name;
      });
    });
  
    return components;
  };

1
投票

我用纯 Javascript 编写了这个函数,以从

google.maps.GeocoderAddressComponent
对象中获取相应的类型

//gets "street_number", "route", "locality", "country", "postal_code"
function getAddressComponents(components, type) {    

    for (var key in components) {
        if (components.hasOwnProperty(key)) {            

            if (type == components[key].types[0]) {
                return components[key].long_name;
            }  
        }        
    }
}

1
投票

这是一个简单的建议:

function parseGoogleResponse (components) {
  var newComponents = {}, type;
  $.each(components, function(i, component) {
    type = component.types[0];
    newComponents[type] = {
      long_name: component.long_name,
      short_name: component.short_name
    }
  });
  return newComponents;
}

用法是:

var components = parseGoogleResponse( places[0].address_components );


1
投票

简单ES6版本:

const addComponents = gPlaceObj?.place?.address_components

然后使用

filter
选择所需的每个组件:

const country = addComponents.filter(x => x?.types?.includes('country'))[0];

这会给你这个

console.log(country)

{"long_name": "United States", "short_name": "US", "types": ["country", "political"]}


0
投票

可以使用以下代码片段获取特定地点的城市、州和国家:

    val addressComps = place.addressComponents
    if (addressComps != null) {
        val addressCompList = addressComps.asList()
        addressCompList.forEach {
            when(it.types[0]) {
                "country" -> country = it.name
                "administrative_area_level_1" -> state = it.name
                "locality" -> city = it.name
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.