2012-06-19 3 views
3

내 응용 프로그램에서 위치 모니터링을 사용하려고합니다. 내 위치를 설정하고 모니터 할 위치를 역으로 바꿀 수 있습니다. didUpdateToLocation 대리자 메서드가 제대로 작동하고 계속 업데이트되지만 didStartMonitoingForRegion 대리자는 호출되지 않으며 didExitRegion이나 didEnterRegion도 호출되지 않습니다.CLLocation 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, 셀 타워 및 위치를 요청하는 전화의 다른 애플리케이션을 사용하는 Apple의 숨겨진 알고리즘에 의해 결정됩니다. 시뮬레이터로 보는 것은 Wi-Fi 나 셀 타워를 사용하지 않습니다 ... 지역 모니터링은 거의 불가능합니다.

코드가 좋을 수도 있지만 (오류는 없습니다) 시뮬레이터가 잘못된 결과를 제공합니다. 기기에서 사용해보고 똑같은 기능을 수행하는지 알려주세요.

+0

휴대 전화에서 앱이 잘 작동하지만 가끔 지오 펜스 횡단을 놓칠 수 있습니다. 그러나 새로운 "울타리"를 설정하면 올바르게 업데이트를 호출하더라도 거의 didStartMonitoingForRegion에 대한 호출이 표시되지 않습니다. – mflac

관련 문제