Ios CLLocation Manager未调用didStartMonitoringForRegion委托方法
我正在尝试在我的应用程序中使用位置监控。我可以设置我的位置并反向编码我要监视的位置。Ios CLLocation Manager未调用didStartMonitoringForRegion委托方法,ios,geolocation,Ios,Geolocation,我正在尝试在我的应用程序中使用位置监控。我可以设置我的位置并反向编码我要监视的位置。didUpdateToLocationdelegate方法工作正常,并持续更新,但从未调用didStartMonitoringForRegion委托,也未调用dideExitRegion或dideEnterRegion 有什么建议吗 - (IBAction)setLocation:(UIButton *)sender { if(!self.locationManager) self.locationMan
didUpdateToLocation
delegate方法工作正常,并持续更新,但从未调用didStartMonitoringForRegion
委托,也未调用dideExitRegion
或dideEnterRegion
有什么建议吗
- (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);
}
}];
}
您是否使用模拟器来测试您的应用程序?我发现模拟器对于区域退出/进入是完全不可靠的。尝试在您的设备上编译项目,方法是将其插入并从iPhone模拟器更改为iOS设备。然后,您可以拔下设备并从手机上运行应用程序来测试它 为什么模拟器不适用于区域?区域主要由苹果的隐藏算法决定,该算法使用Wi-Fi、手机发射塔和手机上请求位置的其他应用程序。因为模拟器不使用Wi-Fi或手机发射塔。。。区域监控将是非常不可能的
您的代码可能很好(我没有看到明显的错误),但模拟器给您带来了糟糕的结果。在您的设备上试用,并让我们知道它是否具有相同的功能。该应用程序在我的手机上运行良好,但偶尔仍会错过地理围栏穿越。然而,当我设置一个新的“围栏”时,我很少看到对DidStartMonitoringForRegion的调用,即使它正确地调用了更新。