iOS 获取当前城市
来源:互联网 发布:淘宝物流信息不更新 编辑:程序博客网 时间:2024/05/16 10:51
#import
2.实现定位协议CLLocationManagerDelegate
3.定义定位属性
@property(nonatomic,retain)CLLocationManager *locationManager;
4.开始定位
- (void)locate
{
// 判断定位操作是否被允许
if([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init] ;
self.locationManager.delegate = self;
}else {
//提示用户无法进行定位操作
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
@"提示" message:@"定位不成功 ,请确认开启定位" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
// 开始定位
[self.locationManager startUpdatingLocation];
}
5.实现定位协议回调方法
#pragma mark - CoreLocation Delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
{
if (array.count > 0)
{
CLPlacemark *placemark = [array objectAtIndex:0];
//将获得的所有信息显示到label上
NSLog(@"%@",placemark.name);
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
self.cityName = city;
}
else if (error == nil && [array count] == 0)
{
NSLog(@"No results were returned.");
}
else if (error != nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
if (error.code == kCLErrorDenied) {
// 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
}
}
- iOS获取当前城市
- iOS 获取当前城市
- iOS 手机定位 获取当前城市
- 获取当前城市
- 获取当前城市
- ios 定位当前城市
- iOS 定位当前城市
- ios定位当前城市
- 从零开始系列之iOS地图获取当前城市
- iOS定位-利用CoreLocation.framework获取当前城市
- 获取当前城市的城市名
- Android获取当前城市名称
- Unity获取当前城市天气
- 如何获取用户当前城市?
- iOS 获取城市IP
- 前后台获取当前用户城市【根据IP获取城市】
- ios项目开发(天气预报项目):通过经纬度获取当前城市名称
- CLGeocoder获取当前所在的城市名
- 【OC刨根问底】-Runtime简单粗暴理解
- vim方便的管理
- MapReduce作业运行流程整体分析
- 记一次基于Unity的Profiler性能分析
- uialert IOS8
- iOS 获取当前城市
- POJ 1228 (计算几何 凸包)
- Dll制作和使用教程二
- [Phonegap+Sencha Touch] 移动开发69 Phonegap/Cordova、Sencha touch的一些官方文档、博客、说明地址
- iPhone/iPad用iTunes“同步”不等于“备份”
- ios 7 statusbar 状态栏 跟随navigationbar的颜色而改变
- 实例化只包含私有带参构造函数对象
- Java 跳出单个循环、多重嵌套循环
- 常用图像压缩方法的对比