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
原创粉丝点击