我正在使用Google地图返回位置名称以及用户所在位置的状态。这是我感兴趣的部分:
"address_components" : [
{
"long_name" : "Salem",
"short_name" : "Salem",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Rockingham",
"short_name" : "Rockingham",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New Hampshire",
"short_name" : "NH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
]
有没有办法找到哪些对象包含“locality”和“administrative_area_level_1”值,并使用JavaScript或jQuery返回相应对象的“long_name”字符串?我该怎么做?
答案 0 :(得分:3)
你去吧。请找小提琴here
使用如下功能:
function getLocality(){
for(var i = 0 ; i< address_components.length; i++){
var obj = address_components[i];
var arr = obj["types"];
for(var j = 0; j<arr.length;j++ ){
if(arr[j] == "locality"){
return obj;
}
}
}
}
或者更确切地说编写相同的Array原型
Array.prototype.getByType = function(type){
for(var i = 0 ; i< address_components.length; i++){
var obj = address_components[i];
var arr = obj["types"];
for(var j = 0; j<arr.length;j++ ){
if(arr[j] == type){
return obj;
}
}
}
}
使用原型如下:
address_components.getByType("administrative_area_level_1").long_name // Note that you pass the type to the prototype function that does the job for you
其中address_components是Google地图返回的数组
答案 1 :(得分:2)
您可以使用此JS库,而不是污染您的代码; DefiantJS(http://defiantjs.com),它使用“搜索”方法扩展全局对象JSON。此方法使您可以使用XPath查询语法搜索JSON结构。结果是您的代码变得更加清晰和可读。
这是工作小提琴;
http://jsfiddle.net/hbi99/SjvZ9/
var data = {
...
},
res = JSON.search( data, '//*[types="administrative_area_level_1" or types="locality"]' ),
str = '';
for (var i=0; i<res.length; i++) {
str += res[i].long_name +'<br/>';
}
document.getElementById('output').innerHTML = str;
不熟悉XPath?查看这个XPath评估器,给你一个简单的提示; http://www.defiantjs.com/#xpath_evaluator
答案 2 :(得分:1)
试试这个:
var data = {"address_components" : [
{
"long_name" : "Salem",
"short_name" : "Salem",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Rockingham",
"short_name" : "Rockingham",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New Hampshire",
"short_name" : "NH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
]};
for(var i = 0; i < data.address_components.length; i++){
if(data[i].long_name == 'something'){
// do something
}
//or
if(data[i].types[0] == 'administrative_area_level_1'){
// do something
}
}
答案 3 :(得分:1)
试试这个,
var addr_comp = [{"long_name": "Salem","short_name": "Salem","types": ["locality", "political"]}, {"long_name": "Rockingham","short_name": "Rockingham","types": ["administrative_area_level_2", "political"]}, {"long_name": "New Hampshire","short_name": "NH","types": ["administrative_area_level_1", "political"]}, {"long_name": "United States","short_name": "US","types": ["country", "political"]}];
function getlocalityAdminLevel(addrType){
for(var a=0,len=addr_comp.length;a<len;a++){
ac=addr_comp[a];
if(ac.types){
for (var t = 0, tl = ac.types.length; t < tl; t++) {
ty = ac.types[t];
if (ty == addrType) {
return ac;break;
}
}
}
}
}
console.log(getlocalityAdminLevel('locality'));
console.log(getlocalityAdminLevel('administrative_area_level_1'));
您可以像使用
一样使用long_name
和short_name
locality=getlocalityAdminLevel('locality');
if(locality && locality.long_name){
alert(locality.long_name);
}