iOS8 定位
2016-03-01 00:00
330 查看
#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface ANNViewController : UIViewController <CLLocationManagerDelegate> @end #import "ANNViewController.h" @interface ANNViewController () @property (strong, nonatomic) IBOutlet UILabel *longitude; @property (strong, nonatomic) IBOutlet UILabel *latitude; @property (strong, nonatomic) IBOutlet UILabel *location; @property (strong, nonatomic) CLLocationManager *locationManager; @end @implementation ANNViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; //ios 8后 //在Info.plist中加入两个缺省没有的字段 //NSLocationAlwaysUsageDescription //NSLocationWhenInUseUsageDescription //创建CLLocationManager对象 self.locationManager = [[CLLocationManager alloc] init]; //设置代理为自己 if ([CLLocationManager locationServicesEnabled]) { NSLog( @"Starting CLLocationManager" ); self.locationManager.delegate = self; [self.locationManager requestAlwaysAuthorization]; self.locationManager.distanceFilter = 200; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; [self.locationManager startUpdatingLocation]; } else { NSLog( @"Cannot Starting CLLocationManager" ); /*self.locationManager.delegate = self; self.locationManager.distanceFilter = 200; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [self.locationManager startUpdatingLocation];*/ } } - (IBAction)locationButton:(UIButton *)sender { [self.locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { //将经度显示到label上 self.longitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.longitude]; //将纬度现实到label上 self.latitude.text = [NSString stringWithFormat:@"%lf", newLocation.coordinate.latitude]; // 获取当前所在的城市名 CLGeocoder *geocoder = [[CLGeocoder alloc] init]; //根据经纬度反向地理编译出地址信息 [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error) { if (array.count > 0) { CLPlacemark *placemark = [array objectAtIndex:0]; //将获得的所有信息显示到label上 self.location.text = placemark.name; //获取城市 NSString *city = placemark.locality; if (!city) { //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市) city = placemark.administrativeArea; } NSLog(@"city = %@", 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:(nonnull NSError *)error { NSLog(@"%@",error); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- ios tableview section的运用
- ios 触摸事件
- IOS学习 ModalViewController模态视图的创建 两视图间用代理方式<传值>
- iOS 沙盒
- 【iOS9系列】- CoreSportlight内容索引的使用
- iOS-地图开发 Plist文件设置权限
- iOS-layoutSubvies和drawRect何时调用
- iOS中block介绍(三)揭开神秘面纱(上)
- iOS-去除NavigationBar边线
- iOS-本地沙盒路径
- iOS 各种单例写法、详解
- iOS-Pods里三方文件导入找不到头文件
- 使用 SourceTree 管理ios项目
- iOS应用崩溃日志分析
- ios学习笔记(二)--代理设计模式
- ios 国际化及默认语言设置
- iOS将字符串3DES加密后转Base64
- iOS开发中邮箱,电话号码,身份证,密码,昵称正则表达式验证
- iOS使用MD5加密
- iOS中的 ARC 与 MRC混编