3

我想制作一个应用程序,为我提供半径为 10 米的当前位置的进入和退出警报消息。这是我的代码:

-(void)startLocationServices
{
    if (self.locationManager == nil)
    {
        self.locationManager = [CLLocationManager new];
    }
    [self.locationManager setDelegate:self];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone];
    [self.locationManager startUpdatingLocation];
}

-(void)stopLocationServices
{
    [self.locationManager stopUpdatingLocation];
    [self.locationManager setDelegate:nil];

    //self.locationUpdateTextView.text = @"Location Service stop";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startLocationServices];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    self.locationUpdateTextView.text = [@"New Location: \n\n" stringByAppendingFormat:@"%@ \n \nCurrent Position\nLatitude: %f \nLongitude: %f", [locations lastObject], self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);

    CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:10.0 identifier:@"Test"];

    [self.locationManager startMonitoringForRegion:region];
    [self stopLocationServices];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Status" message:@"You Enter the region" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

    [alertView show];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Status" message:@"You Exit the region" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

    [alertView show];
}

- (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    self.availabilityTextView.text = [@"\nError:" stringByAppendingFormat:@"%@", error];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

我调用方法[self stopLocationServices];didUpdateLocations以便它获取用户的当前位置,设置半径然后停止。当我运行应用程序并移动 10 米时,尽管我越过 10 米半径,但它没有发出任何警报消息。相反,我随机得到它,特别是当我在跨越 10 米半径后重新启动应用程序时。我知道这种情况下的手机信号塔问题,但我不确定我的想法在这种情况下是否准确可行。如果有人理解我的问题并知道任何解决方案,请与我分享。

4

0 回答 0