对不起,我需要帮助。
这是我的目标(我无法复制&&& firebug)
与此相同(“Json输出格式”段落):https://developers.google.com/maps/documentation/geocoding/
{
"results": [
{
"address_components": [
{
"long_name": "1600",
"short_name": "1600",
"types": ["street_number"]
},
{
"long_name": "Amphitheatre Pkwy",
"short_name": "Amphitheatre Pkwy",
"types": ["route"]
},
{
"long_name": "Mountain View",
"short_name": "Mountain View",
"types": ["locality", "political"]
},
{
"long_name": "Santa Clara",
"short_name": "Santa Clara",
"types": ["administrative_area_level_2", "political"]
},
{
"long_name": "California",
"short_name": "CA",
"types": ["administrative_area_level_1", "political"]
},
{
"long_name": "United States",
"short_name": "US",
"types": ["country", "political"]
},
{
"long_name": "94043",
"short_name": "94043",
"types": ["postal_code"]
}
],
"formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry": {
"location": {
"lat": 37.42291810,
"lng": -122.08542120
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 37.42426708029149,
"lng": -122.0840722197085
},
"southwest": {
"lat": 37.42156911970850,
"lng": -122.0867701802915
}
}
},
"types": ["street_address"]
}
],
"status": "OK"
}
我需要迭代address_components并提取administrative_area_level_2,如果不存在administrative_area_level_3则放入变量(并非所有地址都有3级,在这种情况下,在意大利,你可以使用2级)。
所以,我正在尝试这个:
$.each(result.address_components, function(index, types) {
//FROM HERE PSEUDO CODE... PLEASE HELP
if (types.type[0]['administrative_area_level_3'] == NULL)
{
var administrative_area_level_3 = types.type['administrative_area_level_2'];
}
});
因此,对于我的图像,行政区域3级和2级将具有相同的值(罗马)。
拜托,你能帮帮我吗?非常感谢你
答案 0 :(得分:0)
试试这个,
if (types.type[0]['administrative_area_level_3']==NULL)
-------------------------------------^
而不是
if (types.type[0]['administrative_area_level_3'])==NULL
答案 1 :(得分:0)
尝试并尝试,这是“解决方案”:
var area2 = null;
var area3 = null;
$.each(result.address_components, function(index, types){
if (types.types[0]=='administrative_area_level_2')
{
area2 = types.long_name;
}
if (types.types[0]=='administrative_area_level_3')
{
area3 = types.long_name;
}
});
if (area3==null)
{
area3 = area2;
$('input[name=administrative_area_level_3]').val(area3);
}