gpt4 book ai didi

ios - 未调用 CLLocation 管理器 didStartMonitoringForRegion 委托(delegate)方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:02 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中使用位置监控。我可以设置我的位置和 reverseGeocode 我想监控的位置。 didUpdateToLocation 委托(delegate)方法工作正常,并不断更新,但 didStartMonitoingForRegion 委托(delegate)永远不会被调用,didExitRegiondidEnterRegion.

有什么建议吗?

- (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 设备,尝试在您的设备上编译项目。然后,您可以拔下设备并从手机运行您的应用程序以对其进行测试。

为什么模拟器不适用于区域?区域主要由 Apple 的隐藏算法确定,该算法使用 Wi-Fi、手机信号塔和手机上请求位置的其他应用程序。由于模拟器不使用 Wi-Fi 或手机信号塔……区域监控将变得几乎不可能。

很可能您的代码没有问题(我没有看到明显的错误),但模拟器给您的结果很糟糕。在您的设备上试用它,如果效果相同,请告诉我们。

关于ios - 未调用 CLLocation 管理器 didStartMonitoringForRegion 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11093846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com