当前位置iOS 7的区域监控

时间:2014-02-04 10:09:58

标签: ios7 xcode5 core-location geofencing region-monitoring

我想制作一款能够让我进入Enter&退出当前位置的警报消息,半径为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米半径后重新启动应用程序。在这种情况下,我知道细胞塔问题,但我不确定我的想法在这种情况下是否准确可行。如果有人理解我的问题并且知道任何解决方案,请与我分享。

0 个答案:

没有答案