我正在構建一個專門針對基於位置的應用程序的應用程序,因此我需要獲取用戶座標經度和緯度及其工作。CoreLocation region delegates not called
現在我需要知道用戶是否使用CLLocationManager
代表輸入了預定義的某個區域,因此我使用NSLog
語句覆蓋didEnterRegion
以瞭解用戶是否輸入區域。
在
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
我叫startMonitoringForRegion:
但像didEnterRegion
或monitoringDidFailForRegion
代表或爲任何CLRegion
代表沒有發生。
下面是我的一些代碼:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
locLongittude=newLocation.coordinate.longitude;
locLattiude=newLocation.coordinate.latitude;
self.regionTimer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(monitorRegion) userInfo:nil repeats:NO];
}
-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)radius
{
CLLocationCoordinate2D home;
home.latitude = latitude;
home.longitude = longitude;
CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:radius identifier:@"home"];
if([CLLocationManager regionMonitoringEnabled] && [CLLocationManager regionMonitoringAvailable])
[locManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
NSLog(@"Count: %d region aval %d enable %d",[[locManager monitoredRegions]count], [CLLocationManager regionMonitoringAvailable],[CLLocationManager regionMonitoringEnabled]);
for(CLRegion* region in locManager.monitoredRegions)
NSLog(@"%@", region);
}
-(void)monitorRegion
{
[self startMonitor:31.971160 longitude:35.832465 radius:10000.0];
}
編輯:使用模擬器進行模擬和使用產品 - >調試的位置,所有的測試 - >模擬位置
玉傢伙它的工作原理。上面的代碼很好用,問題在於使didEnterRegion
方法被調用的方法是在您需要輸入的區域之外,然後輸入它以使其工作。
我在做的是在模擬中,我總是選擇一個座標,在我需要輸入的區域內,這是它必須在區域外的問題。
因此,這裏是我的代碼與您分享
更新:我很奇怪,爲什麼當我離開該地區didExitRegion不會被調用?
你能打印出地區,所以你知道他們被添加到監視器堆棧? – Jake
是它添加(現在我只有一個區域) 2012-06-05 12:30:15.929 GPS_Location [1415:f803](identifier home)<+ 31.97116089,+ 35.83246613> radius 10000.00m –