iOS项目开发实战——使用CoreLocation实现定位
来源:互联网 发布:淘宝lolita店 编辑:程序博客网 时间:2024/06/10 07:57
CoreLocation是苹果官方提供的一个框架,可以实现很多地理位置操作上的功能。比如地图显示,定位,地理位置编码等。现在我们来实现定位功能。
(1)代码实现如下:
#import "ViewController.h"#import <MapKit/MapKit.h>@interface ViewController ()<CLLocationManagerDelegate>@property (nonatomic,strong) MKMapView *mapView;@property (nonatomic,strong) CLLocationManager *locationManager;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; [_mapView setShowsUserLocation:true]; [_mapView setMapType:MKMapTypeStandard]; [self.view addSubview:_mapView]; //监测定位功能是否开启 if ([CLLocationManager locationServicesEnabled]) { NSLog(@"已经开启定位"); if (!_locationManager) { _locationManager = [[CLLocationManager alloc] init]; if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [_locationManager requestWhenInUseAuthorization]; [_locationManager requestAlwaysAuthorization]; } //设置代理; [_locationManager setDelegate:self]; //设置定位精度; [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; //设置距离筛选; [_locationManager setDistanceFilter:100]; //开始定位; [_locationManager startUpdatingLocation]; } }else{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您没有开启定位功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; } }//授权状态发生改变的时候;-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ switch (status) { case kCLAuthorizationStatusDenied: { NSLog(@"拒绝了授权"); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您已经拒绝了授权" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; } break; default: break; } }//定位成功以后调用-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *location = locations.lastObject; MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), MKCoordinateSpanMake(0.1, 0.1)); [_mapView setRegion:[_mapView regionThatFits:coordinateRegion] animated:true]; }//定位失败以后调用-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSLog(@"error:%@",error); }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
(2)在模拟器下运行,效果如下:本人在中国,却给我定位到美国了,可见使用模拟器来测试非常的不靠谱。
。
下面是在真机中运行,定位较为准确:
。
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
1 0
- iOS项目开发实战——使用CoreLocation实现定位
- iOS开发 -- CoreLocation实现定位
- iOS项目开发实战——使用CoreLocation获取当前位置信息
- iOS开发—02CoreLocation定位服务
- iOS开发之使用系统库CoreLocation实现定位功能
- iOS开发 CoreLocation实现定位服务
- iOS 使用CoreLocation定位
- iOS-使用CoreLocation定位
- iOS项目开发实战——CoreLocation地理编码和反地理编码
- iOS开发拓展篇—CoreLocation定位服务
- iOS开发拓展篇—CoreLocation定位服务
- iOS开发拓展篇—CoreLocation定位服务
- iOS开发拓展篇—CoreLocation定位服务
- iOS开发拓展篇—CoreLocation定位服务
- iOS开发拓展篇—CoreLocation定位服务
- iOS开发拓展篇—CoreLocation定位服…
- iOS开发拓展篇—CoreLocation定位服务
- iOS开发拓展篇—CoreLocation定位服务
- Mac Python 操作 粘贴板
- Codevs 1226 倒水问题 题解
- VS2012+LUA环境搭建
- 动态空间管理
- 相遇
- iOS项目开发实战——使用CoreLocation实现定位
- 花生壳突然不能用怎么办
- postman(Chrome插件)——接口测试
- 使用SGD(Stochastic Gradient Descent)进行大规模机器学习
- Unity3D游戏开发Mecanim动画系统讲解
- Matlab导出图片--高分辨率且保持线形可辨认
- Best Time to Buy and Sell Stock I II III IV
- 变量在内存中的存储原则
- 《机器学习》学习笔记-第一周