半径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 メートルを超えた後にアプリを再起動すると、ランダムに取得されます。この場合のセルタワーの問題は知っていますが、このシナリオで私の考えが正確に機能するかどうかはわかりません。誰かが私の問題を理解し、解決策を知っている場合は、それを私と共有してください。