用CLLocationManager得到当前城市和国家的名称。

问答小助手 2013-03-15 01:25:41
加精
原问题来自于CSDN问答频道,详细解决方案见:http://ask.csdn.net/questions/1706

问题描述:
通过使用CLLocationManager得到当前城市和国家的名称。
解决方案:
- (void) getReverseGeocode
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];

if(currentLatLong.count > 0)
{
CLLocationCoordinate2D myCoOrdinate;

myCoOrdinate.latitude = LatValue;
myCoOrdinate.longitude = LangValue;

CLLocation *location = [[CLLocation alloc] initWithLatitude:myCoOrdinate.latitude longitude:myCoOrdinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
if (error)
{
NSLog(@"failed with error: %@", error);
return;
}
if(placemarks.count > 0)
{
NSString *MyAddress = @"";
NSString *city = @"";

if([placemark.addressDictionary objectForKey:@"FormattedAddressLines"] != NULL)
MyAddress = [[placemark.addressDictionary objectForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
else
MyAddress = @"Address Not founded";

if([placemark.addressDictionary objectForKey:@"SubAdministrativeArea"] != NULL)
city = [placemark.addressDictionary objectForKey:@"SubAdministrativeArea"];
else if([placemark.addressDictionary objectForKey:@"City"] != NULL)
city = [placemark.addressDictionary objectForKey:@"City"];
else if([placemark.addressDictionary objectForKey:@"Country"] != NULL)
city = [placemark.addressDictionary objectForKey:@"Country"];
else
city = @"City Not founded";

NSLog(@"%@",city);
NSLog(@"%@", MyAddress);
}
}];
}
}
...全文
3779 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
w565464189 2015-11-16
  • 打赏
  • 举报
回复
mark,好银好银
isuhe工作室 2013-03-30
  • 打赏
  • 举报
回复
IOS6以后,苹果地图reverseGeocodeLocation 解析效果极差,50%以上城市名称都找不到,请问还有何好方法获取所在城市名? 怀念谷歌地图时代。。。
u010003433 2013-03-23
  • 打赏
  • 举报
回复
这个程式看起来对我比较难
  • 打赏
  • 举报
回复
多谢 多谢 有用
lhw7791086 2013-03-20
  • 打赏
  • 举报
回复
学习了
无聊客 2013-03-20
  • 打赏
  • 举报
回复
感谢分享!多谢多谢!
albertausun 2013-03-20
  • 打赏
  • 举报
回复
谢谢提供!谢谢分享!
wx8310 2013-03-19
  • 打赏
  • 举报
回复
好分享,好分享!!!我晕,为什么非要6个字!!!
john1159 2013-03-18
  • 打赏
  • 举报
回复
买噶的···好家伙
该用户比较懒 2013-03-18
  • 打赏
  • 举报
回复
看不懂 但是很厉害的样子
aiyun721 2013-03-18
  • 打赏
  • 举报
回复
貌似不错 看不懂 但是很厉害的样子
hh125742 2013-03-16
  • 打赏
  • 举报
回复
积分怎么还没有呢?
hewenti 2013-03-15
  • 打赏
  • 举报
回复
顶一个顶一个~
qqthriller 2013-03-15
  • 打赏
  • 举报
回复
不太懂,顶一个
zhzhxtrrk 2013-03-15
  • 打赏
  • 举报
回复
这个办法很好,得顶一下
tdxhaha123 2013-03-15
  • 打赏
  • 举报
回复
多谢分享!哈哈~
C_S_D_N123123123 2013-03-15
  • 打赏
  • 举报
回复
打转的风铃 2013-03-15
  • 打赏
  • 举报
回复
好分享,好分享!!!我晕,为什么非要6个字!!!
yuyuyuzhu 2013-03-15
  • 打赏
  • 举报
回复
哈哈哈哈~~~
limiao1988329 2013-03-15
  • 打赏
  • 举报
回复
加载更多回复(4)

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧