2012-06-19 80 views
3

我想在我的應用程序中使用位置監視。我可以設置我的位置,並將我想監視的位置逆向地理編碼。 didUpdateToLocation委託方法工作正常,並且不斷更新,但didStartMonitoingForRegion委派永遠不會被調用,也不會執行didExitRegiondidEnterRegionCLLocation Manager didStartMonitoringForRegion不調用的代理方法

有什麼建議嗎?

- (IBAction)setLocation:(UIButton *)sender { 
    if(!self.locationManager) self.locationManager = [[CLLocationManager alloc] init]; 
    [self.locationManager setDelegate:self]; 
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    [self.locationManager setDistanceFilter:10]; // Update again when a user moves  distance in meters 

    [self.locationManager setPurpose:@"Set location based alerts if switch is on"]; 
    self.plugLocation=nil; //reset to nil so didUpdateToLocation will update it 
    self.distanceLabel.text=[NSString stringWithFormat:@"%d",0]; 

    [self.locationManager startUpdatingLocation]; 


    if (![CLLocationManager regionMonitoringAvailable] || ![CLLocationManager regionMonitoringEnabled]){ 
     NSLog(@"Location monitoring not Unavailable"); 
    }else { 

    } 
} 
-(void)setPlugLocation:(CLLocation *)plugLocation{ 
    // 
    if (!_plugLocation) _plugLocation=[[CLLocation alloc]init]; 
     _plugLocation=plugLocation; 
    if (_plugLocation) { 
     [self setRegion:plugLocation radius:20 name:self.plugName]; 
     [self reverseGeocode:self.plugLocation]; 
     NSLog(@"setPlugLocation %@", [self.plugLocation description]); 
      } 
} 

-(void)setRegion:(CLLocation *)center radius:(double)meters name:(NSString*)name{ 
    CLLocationCoordinate2D plug2D; 
    plug2D.latitude=center.coordinate.latitude; 
    plug2D.longitude=center.coordinate.longitude; 
    CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:plug2D radius:meters identifier:name]; 
    [self.locationManager startMonitoringForRegion:region  desiredAccuracy:kCLLocationAccuracyBest]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{  
    self.latitudeLabel.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude]; 
    self.longitudeLabel.text=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]; 

    if (self.plugLocation == nil)self.plugLocation = newLocation; 
    if (self.plugLocation!=nil) { 
    CLLocationDistance distanceBetween = [newLocation distanceFromLocation:self.plugLocation]; 
    NSLog(@"didUpdateToLocation Distance from plug=%f",distanceBetween); 
    self.distanceLabel.text=[NSString stringWithFormat:@"%f",distanceBetween]; 
    } 
} 

-(void)reverseGeocode:(CLLocation *)coordinates;{ 
    if (!self.plugGeo) self.plugGeo = [[CLGeocoder alloc] init]; 
[self.plugGeo reverseGeocodeLocation:coordinates completionHandler:^(NSArray *placemarks, NSError *error) { 
    if (error==nil&&[placemarks count]>0) { 
     CLPlacemark *placemark=[placemarks objectAtIndex:0]; 
     NSLog(@" setPlugLocation geocodeAddressString %@",placemark); 
     //should also transfer back to plug detail and save 
     self.locationLabel.text=[NSString stringWithFormat:@"%@ %@\n%@, %@", placemark.subThoroughfare, placemark.thoroughfare, placemark.locality,placemark.postalCode]; 
     [self sendAlertMessage:[NSString stringWithFormat:@"%@ %@\n%@, %@", placemark.subThoroughfare, placemark.thoroughfare, placemark.locality,placemark.postalCode] title:@"Success"]; 
    }else { 
     NSLog(@" setPlugLocation couldn't geoCode address %@",error); 
    } 
}]; 
} 

回答

0

您是否在使用模擬器測試您的應用程序?我發現的東西是模擬器對於區域出口/入口完全不可靠。嘗試在設備上編譯項目,插入並從iPhone Simulator更改爲iOS設備。然後,您可以拔下設備並從手機運行您的應用程序進行測試。

爲什麼模擬器不適用於區域?地區主要取決於蘋果公司使用Wi-Fi,手機信號塔以及手機上其他應用程序請求位置的隱藏算法。看到模擬器不使用Wi-Fi或手機塔...區域監控將是不可能的。

這可能是你的代碼很好(我沒有看到明顯的錯誤),但模擬器給你的結果不好。在您的設備上嘗試一下,並告訴我們它是否也執行相同的操作。

+0

該應用程序在我的手機上運行良好,但它偶爾會錯過地理圍欄過境點。但是,當我建立一個新的「圍欄」時,即使它正確調用更新,我也很少看到對didStartMonitoingForRegion的調用。 – mflac

相關問題