面临解析谷歌位置响应的困难 [英] Facing difficulty in parsing google location response
问题描述
使用iOS 8.0。
我使用 Google API
来获取位置,并且我想减少使用NSPredicate和KeyPath进行编码。
我使用下面的链接获取google响应,
iPhone - 从Latitude和Longtiude获取城市名称
使用user @'的答案 Constantin Saulenco '。
如果我想要一个Sql开发人员,我会编写一个查询,例如
pre > 1.(从MAIN_TABLE中选择address_components,其中类似'postal_code')从这个我将得到NAME_TABLE行。
2.从NAME_TABLE中选择long_name,其中类型为'locality'
我在下面的代码中使用,在看完 @ztan 的答案后,我改变了这种编码方式
编辑:回答
NSArray * addressArray = json [@results];
NSPredicate * predicateForTypes = [NSPredicate predicateWithFormat:@(types CONTAINS'postal_code')];
addressArray =([addressArray filteredArrayUsingPredicate:predicateForTypes] [0])[@address_components];
NSPredicate * predicateForCity = [NSPredicate predicateWithFormat:@(types CONTAINS'locality')];
NSString * cityName =([addressArray filteredArrayUsingPredicate:predicateForCity] [0])[@long_name];
我有JSON以下...
{
address_components=(
{
long_name= 221;
short_name= 221;
类型=(
street_number
);
},
{
long_name=Juniper Drive;
short_name=Juniper Dr ;
types =(
route
);
},
{
long_name=North Kingstown;
short_name =North Kingstown;
types =(
locality,
political
);
},
{
long_name= 华盛顿县;
short_name=华盛顿县;
types =(
administrative_area_level_2,
political
);
},
{
long_name=罗德岛;
short_name= RI;
types =(
administrative_area_level_1,
political
);
},
{
long_name=美国;
short_name= US;
类型=(
国家,
政治
);
},
{
long_name= 02852;
short_name= 02852;
types =(
postal_code
);
},
{
long_name= 4308;
short_name= 4308;
types =(
postal_code_suffix
);
}
);
formatted_address=221 Juniper Drive,North Kingstown,RI 02852,USA;
geometry = {
location = {
lat =41.577761;
lng =-71.468383;
};
location_type= ROOFTOP;
viewport = {
northeast = {
lat =41.5791099802915;
lng =-71.46703401970849;
};
southwest = {
lat =41.5764120197085;
lng =-71.4697319802915;
};
};
};
place_id= ChIJ563yzFex5YkRujoi28Fvzvo;
types =(
street_address
);
},
{
address_components=(
{
long_name=North Kingstown;
short_name=North Kingstown ;
类型=(
地点,
政治
);
},
{
long_name=华盛顿县;
short_name=华盛顿县;
类型=(
administrative_area_level_2,
政治
);
},
{
long_name=罗得岛;
short_name= RI;
类型=(
administrative_area_level_1,
政治
);
},
{
long_name=美国;
short_name= US;
类型=(
国家,
政治
);
}
);
formatted_address=美国罗得岛州北金斯敦;
geometry = {
bounds = {
northeast = {
lat =41.6549521;
lng =-71.4023083;
};
southwest = {
lat =41.497126;
lng =-71.52244109999999;
};
};
location = {
lat =41.5568315;
lng =-71.4536835;
};
location_type= APPROXIMATE;
viewport = {
northeast = {
lat =41.6549521;
lng =-71.4023083;
};
southwest = {
lat =41.497126;
lng =-71.52244109999999;
};
};
};
place_id=ChIJgWUP-V-x5YkRO7Zie7NXWsI;
类型=(
地点,
政治
);
},第二个JSON对象,第三个JSON对象,......,....
我的问题是如何减少编码?使用NSPRedicates和KeyPath ..
这就是我们如何解决问题的方法......我在@ ztan的回答中提到了这个问题,请看看,这将是有益的。
请参阅我的第二条评论.......:)
您可以做以下内容:
NSArray * addressArray = resultDict [@results];
NSPredicate * predicateFromTypes = [NSPredicate predicateWithFormat:@(types CONTAINS'postal_code')AND(types CONTAINS'locality')];
NSLog(@\\\
%@,[addressArray [0] [@address_components] filteredArrayUsingPredicate:predicateFromTypes] [0] [@long_name]);
Using iOS 8.0.
I am using Google API
for getting the location.and I want to reduce the coding using NSPredicate and KeyPath.
I am using below link for getting the google response, iPhone - Get City name from Latitude and Longtiude Using answer of user @'Constantin Saulenco'.
If I would a Sql developer I would write a query like,
1. (select address_components from MAIN_TABLE where types like 'postal_code') from this I will get NAME_TABLE rows.
2.select long_name from NAME_TABLE where types like 'locality'
For that I am using below code,After watching the @ztan's answer I have changed the coding like this
Edit: Answer
NSArray *addressArray = json[@"results"];
NSPredicate *predicateForTypes=[NSPredicate predicateWithFormat:@"(types CONTAINS 'postal_code')"];
addressArray=([addressArray filteredArrayUsingPredicate:predicateForTypes][0])[@"address_components"];
NSPredicate *predicateForCity=[NSPredicate predicateWithFormat:@"(types CONTAINS 'locality')"];
NSString *cityName=([addressArray filteredArrayUsingPredicate:predicateForCity][0])[@"long_name"];
I have below JSON...
{
"address_components" = (
{
"long_name" = 221;
"short_name" = 221;
types = (
"street_number"
);
},
{
"long_name" = "Juniper Drive";
"short_name" = "Juniper Dr";
types = (
route
);
},
{
"long_name" = "North Kingstown";
"short_name" = "North Kingstown";
types = (
locality,
political
);
},
{
"long_name" = "Washington County";
"short_name" = "Washington County";
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = "Rhode Island";
"short_name" = RI;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = "United States";
"short_name" = US;
types = (
country,
political
);
},
{
"long_name" = 02852;
"short_name" = 02852;
types = (
"postal_code"
);
},
{
"long_name" = 4308;
"short_name" = 4308;
types = (
"postal_code_suffix"
);
}
);
"formatted_address" = "221 Juniper Drive, North Kingstown, RI 02852, USA";
geometry = {
location = {
lat = "41.577761";
lng = "-71.468383";
};
"location_type" = ROOFTOP;
viewport = {
northeast = {
lat = "41.5791099802915";
lng = "-71.46703401970849";
};
southwest = {
lat = "41.5764120197085";
lng = "-71.4697319802915";
};
};
};
"place_id" = ChIJ563yzFex5YkRujoi28Fvzvo;
types = (
"street_address"
);
},
{
"address_components" = (
{
"long_name" = "North Kingstown";
"short_name" = "North Kingstown";
types = (
locality,
political
);
},
{
"long_name" = "Washington County";
"short_name" = "Washington County";
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = "Rhode Island";
"short_name" = RI;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = "United States";
"short_name" = US;
types = (
country,
political
);
}
);
"formatted_address" = "North Kingstown, RI, USA";
geometry = {
bounds = {
northeast = {
lat = "41.6549521";
lng = "-71.4023083";
};
southwest = {
lat = "41.497126";
lng = "-71.52244109999999";
};
};
location = {
lat = "41.5568315";
lng = "-71.4536835";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "41.6549521";
lng = "-71.4023083";
};
southwest = {
lat = "41.497126";
lng = "-71.52244109999999";
};
};
};
"place_id" = "ChIJgWUP-V-x5YkRO7Zie7NXWsI";
types = (
locality,
political
);
},2nd JSON Object, 3rd JSON Object ,......,....
My question is How to Reduce the coding ? Using NSPRedicates and KeyPath ..
This is how can we brake the problems......I have mentioned this in @ztan's answer, please have a look, It would be beneficial.
please see my second comment.......:)
You can do the following:
NSArray *addressArray = resultDict[@"results"];
NSPredicate *predicateFromTypes=[NSPredicate predicateWithFormat:@"(types CONTAINS 'postal_code') AND (types CONTAINS 'locality')"];
NSLog(@"\n%@", [addressArray[0][@"address_components"] filteredArrayUsingPredicate:predicateFromTypes][0][@"long_name"]);
这篇关于面临解析谷歌位置响应的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!