Corelocation及地圖控制項學習筆記

來源:互聯網
上載者:User

標籤:

Corelocation基本使用

在地圖章節的學習中,首先要學的便是使用者位置定位,因此我們首先要掌握Corelocation的使用。(在IOS8以前可以系統會直接請求授權,現在需要我們自己調用方式通知系統請求授權)

首先設定一個Corelocation屬性並實現懶載入設定代理,此對象需要自己調用方法startUpdatingLocation及stopUpdatingLocation來開始和結束位置擷取

1 //定位管理者2 @property (nonatomic , strong ) CLLocationManager *manager;3 - (CLLocationManager *)manager{4     if (_manager == nil) {5         //建立CoreLocation管理者6         _manager = [[CLLocationManager alloc] init];
7 _manager.delegate = self;
8 }9 return _manager;10 }

設定好定位管理者對象後就對其屬性進行一些常用的屬性配置

 1 //配置定位精度 2 /* 3 導航層級:     kCLLocationAccuracyBestForNavigation; 4 最優:        kCLLocationAccuracyBest; 5 精確到10米:   kCLLocationAccuracyNearestTenMeters; 6 精確到100米:  kCLLocationAccuracyHundredMeters; 7 精確到1000米: kCLLocationAccuracyKilometer; 8 精確到3000米: kCLLocationAccuracyThreeKilometers; 9 */10 self.manager.desiredAccuracy = kCLLocationAccuracyBest;11 //設定超過範圍後更新資料,如不設定資料會一直不間斷更新12 self.manager.distanceFilter = 100;

若使用[self.manager requestAlwaysAuthorization]方法需要添加NSLocationAlwaysUsageDescription為key

 1 //判斷系統IOS版本 2     if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) { 3         NSLog(@"IOS8"); 4         //注意:IOS8後需要驗證授權,在Info.plist檔案還要加上NSLocationWhenInUseUsageDescription這個key,Value可以為空白,並調用此方法 5         [self.manager requestWhenInUseAuthorization]; 6         [self.manager startUpdatingLocation]; 7     }else{ 8         NSLog(@"IOS7"); 9         //開始擷取使用者位置10         [self.manager startUpdatingLocation];11     }

接下來便需要實現一些代理方法來方便我們擷取使用者位置,以下為常用的部分代理協議

1.使用者授權狀態改變後調用

1 //在此判斷授權狀態並進行相對應的操作2 - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{3     if (status == kCLAuthorizationStatusAuthorizedAlways ||4         status == kCLAuthorizationStatusAuthorizedWhenInUse) {5         NSLog(@"授權成功");6         //授權成功後開始監聽 擷取位置7         [self.manager startUpdatingLocation];8     }9 }

2.在更新使用者位置時調用的方法

 1 //可以在這裡進行反地理編碼擷取位置資訊 2 - (void)locationManager:(CLLocationManager *)managerdidUpdateLocations:(NSArray *)locations{ 3     //反地理編碼使用位置資訊反編碼 4     CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 5     CLLocation *newLocation = [locations lastObject]; 6     [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 7         for (CLPlacemark *place in placemarks) { 8             //通過CLPlacemark可以輸出使用者位置資訊 9             NSLog(@"%@ %@ %lf %lf",place.name, place.addressDictionary, place.location.coordinate.latitude,place.location.coordinate.longitude);10         }11     }];12 }

3.使用者手機頭部方向改變時調用的方法

1 //在更新使用者頭部方向時調用,適用方向指定操作2 - (void)locationManager:(CLLocationManager *)managerdidUpdateHeading:(CLHeading *)newHeading

至此Corelocation的配置就已完成,接下來是關於地圖配置的方法

地圖控制項的基本使用

首先要匯入地圖架構 #import <MapKit/MapKit.h>

設定地圖控制項屬性,並懶載入實現及其屬性配置

 1 //地圖控制項,其中region屬性個人覺得在代理中設定比較合適 2 @property (nonatomic, strong) MKMapView *mapView; 3 - (MKMapView *)mapView{ 4     if (_mapView == nil) { 5         _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 6         _mapView.delegate = self; 7         /* 8          MKUserTrackingModeNone = 0 預設不跟蹤 9          MKUserTrackingModeFollow,  追蹤位置10          MKUserTrackingModeFollowWithHeading 追蹤位置及其頭部指向11          */12         //使用者追蹤模型,如不設定將無法追蹤使用者13         _mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;14         /*15          MKMapTypeStandard = 0,普通模式(預設模式)16          MKMapTypeSatellite,   衛星模式17          MKMapTypeHybrid       混合模式18          */19         _mapView.mapType = MKMapTypeStandard;20     }21     return _mapView;22 }

最後將_mapView添加進self.view中,整個地圖相當於就可以載入完成了。下面介紹MKMapView的一部分代理方法及其調用時機

 1 #pragma mark - MKMapViewDelegate 代理方法 2 /** 3  *  更新使用者位置是調用 4  * 5  *  @param mapView      題圖 6  *  @param userLocation 使用者位置 7  */ 8 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{ 9     NSLog(@"更新使用者位置");10     //設定使用者位置中心點11     [self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];12     //範圍設定MKCoordinateSpanMake中的代表經緯度範圍13     MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.1, 0.1));14     //設定中心點範圍15     [self.mapView setRegion:region animated:YES];16 }17 /**18  *  地圖將要開始渲染時調用19  */20 - (void)mapViewWillStartRenderingMap:(MKMapView *)mapView{21     NSLog(@"%s",__func__);22 }23 /**24  *  地圖完成渲染時調用25  */26 - (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered{27     NSLog(@"%s",__func__);28 }29 /**30  *  region(範圍)即將改變時調用31  */32 - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{33     NSLog(@"%s",__func__);34 }35 /**36  *  region(範圍)完成改變時調用37  */38 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{39     NSLog(@"%s",__func__);40 }41 /**42  *  追蹤模型改變時調用43  */44 - (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated{45     NSLog(@"%s",__func__);46 }47 /**48  *  設定圖釘時調用 類似於tableViewCell設定49  */50 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
51 //在其中設定MKAnnotationVIew,有重用機制
52}

另外還有其他一些代理方法就不一一介紹了,剩下的就看需要 在對應的方法裡添加想進行的操作。

Corelocation及地圖控制項學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.