天天看點

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

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

<a href="http://s3.51cto.com/wyfs02/M01/23/AA/wKiom1M-D-DzZ_JwAABPV7jiVUs866.jpg" target="_blank"></a>

圖9.6臨近警告的示意圖

使用者裝置不斷地臨近指定固定點,當與該固定點的距離小于指定範圍時,系統可以觸發相應的處理。使用者裝置離開指定固定點,當與該固定點的距離大于指定範圍時,系統也可以觸發相應的處理。

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

建立CLLocationManager對象,該對象負責擷取定位相關資訊,并為該對象設定一些必要的屬性。對于區域監測而言,CLLocationManager對象需要設定monitoredRegions屬性,該屬性值用于設定該裝置監聽的多個區域。

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

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

當裝置進入指定區域時,iOS系統将會自動激發CLLocationManager的delegate對象的locationManager:didEnterRegion:方法;當裝置離開指定區域時,iOS系統将會自動激發CLLocationManager的delegate對象的locationManager:didExitRegion:方法,開發者可重寫這兩個方法對使用者進行提醒。

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

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

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

@interface FKViewController ()&lt;CLLocationManagerDelegate&gt;

@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];

// 離開指定區域以後将彈出提示框提示使用者

didExitRegion:(CLRegion *)region

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

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

當該裝置進入或離開指定區域時,iOS系統都會自動激發CLLocationManager的delegate對象的相應方法,上面程式重寫了這兩個方法對使用者進行提醒。

編譯、運作該程式,如果将模拟器的位置設為{23.126272, 113.395568},此時裝置将會進入“瘋狂軟體教育中心區域内”,系統将會顯示如圖9.7所示的提示。

編譯、運作該程式,如果将模拟器的位置設為其他位置,使之離開{23.126272, 113.395568}超過500米,此時裝置将會離開“瘋狂軟體教育中心區域内”,系統将會顯示如圖9.8所示的提示。

<a href="http://s3.51cto.com/wyfs02/M02/23/AA/wKiom1M-D_-iM1-sAABrsuIqDaw454.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M01/23/AB/wKioL1M-D9fwiK6mAABp5OqxSU8595.jpg" target="_blank"></a>

圖9.8離開區域提示

繼續閱讀