iOS專案開發實戰——使用CoreLocation實現定位

乞力馬紮羅的雪CYF發表於2015-09-20

      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  。歡迎大家訪問!


相關文章