当前,我正在使用以下代码来获取国家/地区,邮政编码,地区和子地区:
var country, postal_code, locality, sublocality;
for (i = 0; i < results[0].address_components.length; ++i)
{
for (j = 0; j < results[0].address_components[i].types.length; ++j)
{
if (!country && results[0].address_components[i].types[j] == "country")
country = results[0].address_components[i].long_name;
else if (!postal_code && results[0].address_components[i].types[j] == "postal_code")
postal_code = results[0].address_components[i].long_name;
else if (!locality && results[0].address_components[i].types[j] == "locality")
locality = results[0].address_components[i].long_name;
else if (!sublocality && results[0].address_components[i].types[j] == "sublocality")
sublocality = results[0].address_components[i].long_name;
}
}
这令人不满意。有没有其他方法可以达到相同的效果?
答案 0 :(得分:17)
您可以使用以下函数来提取任何地址组件:
function extractFromAdress(components, type){
for (var i=0; i<components.length; i++)
for (var j=0; j<components[i].types.length; j++)
if (components[i].types[j]==type) return components[i].long_name;
return "";
}
提取您调用的信息:
var postCode = extractFromAdress(results[0].address_components, "postal_code");
var street = extractFromAdress(results[0].address_components, "route");
var town = extractFromAdress(results[0].address_components, "locality");
var country = extractFromAdress(results[0].address_components, "country");
等...
答案 1 :(得分:9)
我的单行使用功能方法和map
,filter
和ES2015:
/**
* Get the value for a given key in address_components
*
* @param {Array} components address_components returned from Google maps autocomplete
* @param type key for desired address component
* @returns {String} value, if found, for given type (key)
*/
function extractFromAddress(components, type) {
return components.filter((component) => component.types.indexOf(type) === 0).map((item) => item.long_name).pop() || null;
}
用法:
const place = autocomplete.getPlace();
const address_components = place["address_components"] || [];
const postal_code = extractFromAddress(address_components, "postal_code");
答案 2 :(得分:8)
您可以将其缩短为
var country, postal_code, locality, sublocality;
for (i = 0; i < results[0].address_components.length; ++i) {
var component = results[0].address_components[i];
if (!sublocality && component.types.indexOf("sublocality") > -1)
sublocality = component.long_name;
else if (!locality && component.types.indexOf("locality") > -1)
locality = component.long_name;
else if (!postal_code && component.types.indexOf("postal_code") > -1)
postal_code = component.long_name;
else if (!country && component.types.indexOf("country") > -1)
country = component.long_name;
}
或者您是否想要获得更好的格式化结果?那么请告诉我们你的问题。
答案 3 :(得分:3)
我是这样做的:
placeParser = function(place){
result = {};
for(var i = 0; i < place.address_components.length; i++){
ac = place.address_components[i];
result[ac.types[0]] = ac.long_name;
}
return result;
};
然后我只使用
parsed = placeParser(place)
parsed.route
答案 4 :(得分:1)
使用underscore.js的访问者可以轻松地将地理编码响应中的address_components
数组转换为对象文字:
var obj = _.object(
_.map(results[0].address_components, function(c){
return [c.types[0], c.short_name]
})
);
答案 5 :(得分:1)
我真的相信上面的user1429980回答值得更多认可。它工作得很好。我的回答是基于他的功能。我添加了一些示例来更好地说明如何使用提供的代码user1429980搜索JSON对象:
//searches object for a given key and returns the key's value
extractFromObject (object, key) {
return object.filter((component) => component.types.indexOf(key)
=== 0).map((item)=>item.long_name).pop() || null;
}
示例1: Google的reverseGeocode API,经度和纬度设置为43.6532,79.3832(加拿大安大略省多伦多市):
var jsonData = {} //object contains data returned from reverseGeocode API
var city = extractFromObject(jsonData.json.results[0].address_components, 'locality');
console.log(city); //Output is Toronto
示例2:地方ID设置为ChIJE9on3F3HwoAR9AhGJW_fL-I(美国加利福尼亚州洛杉矶)的Google的Places API:
var jsonData = {} //object contains data returned from Google's Places API
var city = extractFromObject(jsonData.json.result.address_components, 'locality');
console.log(city); //Output is Los Angeles
答案 6 :(得分:1)
使用lodash
const result = _.chain(json.results[0].address_components)
.keyBy('types[0]')
.mapValues('short_name')
.value()
答案 7 :(得分:0)
if (typeof Object.keys == 'function')
var length = function(x) { return Object.keys(x).length; };
else
var length = function() {};
var location = {};
for (i = 0; i < results[0].address_components.length; ++i)
{
var component = results[0].address_components[i];
if (!location.country && component.types.indexOf("country") > -1)
location.country = component.long_name;
else if (!location.postal_code && component.types.indexOf("postal_code") > -1)
location.postal_code = component.long_name;
else if (location.locality && component.types.indexOf("locality") > -1)
location.locality = component.long_name;
else if (location.sublocality && component.types.indexOf("sublocality") > -1)
location.sublocality = component.long_name;
// nothing will happen here if `Object.keys` isn't supported!
if (length(location) == 4)
break;
}
这对我来说是最合适的解决方案。它也可以帮助别人。
答案 8 :(得分:0)
我在创建函数之前提取了给定位置类型列表的值列表:
const getValue = function(data, types=[]){
/* used by results taken from Geocoder.geocode api */
const values = data.reduce((values, address) => {
return address.address_components.reduce((values2, component) => {
if(component.types.reduce((result, type) => result || types.indexOf(type) > -1, false))
values2.push(component.long_name);
return values2
}, []);
if(buff.length)
return [...values, ...buff];
return values;
}, []).filter(
(value, index, self) => {
return self.indexOf(value) === index;
}
);
}