ios CoreLocation定位服務

來源:互聯網
上載者:User

標籤:

  CoreLocation匯入架構  :#import <CoreLocation/CoreLocation.h>

  需要瞭解的基本的屬性和方法:

  屬性:

  • 定位管理者:CLLocationManager
  • 請求定位許可權:requestAlwaysAuthorization
  • 開始擷取位置:startUpdatingLocation
  • 停止擷取位置:stopUpdatingLocation
  • 授權認證狀態:CLAuthorizationStatus
  • 過濾定位的距離:distanceFilter
  • 定位所需精度:desiredAccuracy
  • 定位到的資訊:CLLocation
  • 建立經緯度點:CLLocationCoordinate2D
      

  方法:

  • 授權狀態發生改變:

    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

  • 擷取到位置資訊:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

  • 進入監聽地區:

    - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

  • 離開監聽地區:

    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region

  

  CoreLocatio定位的基本操作:

  在ios8後,系統不會預設幫我們調用定位授權,需要我們自己主動要求使用者給我們授權,我們需要調用此方法:

  [self.mgr requestAlwaysAuthorization];

  並且我們還需要在info.plist檔案中配置:

  NSLocationWhenInUseDescription,允許在前台擷取GPS的描述

     NSLocationAlwaysUsageDescription,允許在後台擷取GPS的描述

#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate>/** *  定位管理者 */@property (nonatomic ,strong) CLLocationManager *mgr;@end@implementation ViewController// 懶載入// 建立CoreLocation管理者- (CLLocationManager *)mgr{    if (!_mgr) {        _mgr = [[CLLocationManager alloc] init];    }    return _mgr;}- (void)viewDidLoad {    [super viewDidLoad];        // 設定代理監聽擷取到的位置    self.mgr.delegate = self;    // 判斷是否是iOS8    if([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0)    {        NSLog(@"是iOS8");        // 主動要求使用者對我們的程式授權, 授權狀態改變就會通知代理        [self.mgr requestAlwaysAuthorization];     }else    {       // 開始監聽(開始擷取位置)        [self.mgr startUpdatingLocation];    }    }// 授權狀態發生改變時調用- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{   if (status == kCLAuthorizationStatusNotDetermined) {        NSLog(@"等待使用者授權");    }else if (status == kCLAuthorizationStatusAuthorizedAlways ||              status == kCLAuthorizationStatusAuthorizedWhenInUse)            {        NSLog(@"授權成功");        // 開始定位        [self.mgr startUpdatingLocation];            }else    {        NSLog(@"授權失敗");    }}#pragma mark - CLLocationManagerDelegate//  擷取到位置資訊之後就會調用(調用頻率非常高)- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    // 定位的資訊    // CLLocation *location = [locations lastObject];       }

 

ios 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.