iOS CoreLocation之地區監測

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   sp   for   

CoreLocation的地區監測,來自瘋狂iOS講義

 

1.引CoreLocation架構,匯入標頭檔

#import <CoreLocation/CoreLocation.h>

 

2.添加定位管理為成員變數,添加消極式載入

@property (nonatomic,strong) CLLocationManager *locMgr;
 1 /** 2  *  懶載入 3  */ 4 - (CLLocationManager *)locMgr 5 { 6     if (_locMgr == nil) { 7         _locMgr = [[CLLocationManager alloc]init]; 8         _locMgr.delegate = self; 9     }10     return _locMgr;11 }

 

3.開啟地區監測

 1 - (void)viewDidLoad 2 { 3     [super viewDidLoad]; 4  5     // 設定中心 6     CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(39, 116); 7     // 設定半徑 8     CLLocationDistance distance = 500; 9     // 建立監測地區10     CLRegion *region = [[CLCircularRegion alloc]initWithCenter:coordinate radius:distance identifier:@"region"];11     12     // 開始地區檢測13     [self.locMgr startMonitoringForRegion:region];14 }

 

4.添加代理方法

 1 #pragma mark -CLLocationManagerDelegate 2 - (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region 3 { 4     NSLog(@"成功開啟地區監測"); 5 } 6  7 - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region 8 { 9     NSLog(@"你已進入監測地區");10 }11 12 - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region13 {14     NSLog(@"你已經離開監測地區");15     16     // 關閉地區監測17     if ([region.identifier  isEqual: @"region"]) {18         [self.locMgr stopMonitoringForRegion:region];19     }20 21 }

 

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.