瘋狂ios講義之使用CoreLocation定位(4)

spacer.gif9.4區域監測

如果希望iOS設備進入某個區域發出通知,那麼這種區域監測的功能也被稱爲臨近警告。所謂臨近警告的示意圖如圖9.6所示。

wKiom1M-D-DzZ_JwAABPV7jiVUs866.jpg

9.6臨近警告的示意圖

用戶設備不斷地臨近指定固定點,當與該固定點的距離小於指定範圍時,系統可以觸發相應的處理。用戶設備離開指定固定點,當與該固定點的距離大於指定範圍時,系統也可以觸發相應的處理。

iOS的區域監測同樣可以使用CLLocationManager來實現,監聽設備是否進入/離開某個區域的步驟如下。

創建CLLocationManager對象,該對象負責獲取定位相關信息,併爲該對象設置一些必要的屬性。對於區域監測而言,CLLocationManager對象需要設置monitoredRegions屬性,該屬性值用於設置該設備監聽的多個區域。

CLLocationManager指定delegate屬性,該屬性值必須是一個實現CLLocationManagerDelegate協議的對象。實現CLLocationManagerDelegate協議時可根據需要實現協議中特定的方法。

調用CLLocationManagerstartMonitoringForRegion:方法進行區域監測。區域監測結束時,可調用stopMonitoringForRegion:方法結束區域監測。

當設備進入指定區域時,iOS系統將會自動激發CLLocationManagerdelegate對象的locationManager:didEnterRegion:方法;當設備離開指定區域時,iOS系統將會自動激發CLLocationManagerdelegate對象的locationManager:didExitRegion:方法,開發者可重寫這兩個方法對用戶進行提醒。

iOS提供了CLRegion來定義被監測的區域,但實際編程中推薦使用CLCircularRegionCLRegion的子類)創建圓形區域,創建CLCircularRegion對象時無非就是指定圓心、半徑等信息,非常簡單。下面示例會進行詳細示範。

新建一個SingleView Application,該應用無須修改界面設計文件,直接修改視圖控制器類的實現部分來監測設備是否進入、離開某個區域。該示例的視圖控制器類的實現部分代碼如下。

程序清單:codes/09/9.4/RegionMonitor/RegionMonitor/FKViewController.m

@interface FKViewController ()<CLLocationManagerDelegate>

@property (retain,nonatomic) CLLocationManager*locationManager;

@end

@implementation FKViewController

- (void)viewDidLoad

{

[superviewDidLoad];

if([CLLocationManager locationServicesEnabled])

{

self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;

// 定義一個CLLocationCoordinate2D作爲區域的圓心

CLLocationCoordinate2D companyCenter;

companyCenter.latitude = 23.126272;

companyCenter.longitude = 113.395568;

// 使用CLCircularRegion創建一個圓形區域,半徑爲500

CLRegion* fkit = [[CLCircularRegionalloc] initWithCenter:companyCenter

radius:500 identifier:@"fkit"];

// 開始監聽fkit區域

[self.locationManagerstartMonitoringForRegion:fkit];

}

else

{

// 使用警告框提醒用戶

[[[UIAlertView alloc] initWithTitle:@"提醒"

message:@"您的設備不支持定位" delegate:self

cancelButtonTitle:@"確定"otherButtonTitles: nil] show];


}

}

// 進入指定區域以後將彈出提示框提示用戶

-(void)locationManager:(CLLocationManager*)manager

didEnterRegion:(CLRegion *)region

{

[[[UIAlertView alloc] initWithTitle:@"區域檢測提示"

message:@"您已經【進入】瘋狂軟件教育中心區域內!" delegate:nil

cancelButtonTitle:@"OK" otherButtonTitles:nil] show];

}

// 離開指定區域以後將彈出提示框提示用戶

-(void)locationManager:(CLLocationManager*)manager

didExitRegion:(CLRegion *)region

{

[[[UIAlertView alloc] initWithTitle:@"區域檢測提示"

message:@"您已經【離開】瘋狂軟件教育中心區域!" delegate:nil

cancelButtonTitle:@"OK" otherButtonTitles:nil] show];

}

@end


正如上面程序中的粗體字代碼所看到的,第1行粗體字代碼創建了一個CLRegion對象作爲被監測區域,接下來調用CLLocationManagerstartMonitoringForRegion:方法監聽該設備是否進入/離開指定區域。

當該設備進入或離開指定區域時,iOS系統都會自動激發CLLocationManagerdelegate對象的相應方法,上面程序重寫了這兩個方法對用戶進行提醒。

編譯、運行該程序,如果將模擬器的位置設爲{23.126272, 113.395568},此時設備將會進入“瘋狂軟件教育中心區域內”,系統將會顯示如圖9.7所示的提示。

編譯、運行該程序,如果將模擬器的位置設爲其他位置,使之離開{23.126272, 113.395568}超過500米,此時設備將會離開“瘋狂軟件教育中心區域內”,系統將會顯示如圖9.8所示的提示。

wKiom1M-D_-iM1-sAABrsuIqDaw454.jpg

9.7進入區域提示

wKioL1M-D9fwiK6mAABp5OqxSU8595.jpg

9.8離開區域提示


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章