Я запускаю свой код в симуляторе iPhone, и в меню симулятора я выбираю: «Отладка, Расположение, Apple», чтобы установить мое текущее местоположение в расположении Apple в Купертино.
Затем я выбираю из меню симулятора: «Отладка, Расположение, Автострада».Текущее местоположение корректно обновляется вдоль автострады 280.
Мой код должен создать «геозону» длиной 1500 метров.Каждый раз, когда устройство достигает границы - оно бросает булавку и рисует круг.
Проблемы .....
- "didExitRegion" не вызывается на правильном расстоянии,он вызывается в нескольких милях вниз по шоссе.
- "didStartMonitoringForRegion" никогда не вызывается.
Любая помощь приветствуется !!Спасибо !!
Вот мой код:
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MKMapView.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
@property(nonatomic,strong) CLLocationManager *locationManager;
@property(nonatomic,strong) MKMapView *myMapView;
@end
ViewController.m
#import "ViewController.h"
#import "POI.h"
@implementation ViewController
@synthesize locationManager;
@synthesize myMapView;
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)
[locationManager startUpdatingLocation];
myMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[myMapView setMapType:MKMapTypeStandard];
[myMapView setDelegate:self];
[myMapView setShowsUserLocation:YES];
[self.view addSubview:myMapView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{return NO;}
#pragma mark CLLocationManagerDelegate methods
-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{ NSLog(@"%@: %@", @"monitoring failed", error.description); }
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{ NSLog(@"%@", @"monitoring succeeded"); }
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{ NSLog(@"%@: %@", @"region entered", region.identifier);}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"%@: %@", @"region exited", region.identifier);
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if(status == kCLAuthorizationStatusAuthorized)
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", @"New Location");
[locationManager stopUpdatingLocation];
NSInteger radius = 1500;
CLRegion *reg = [[CLRegion alloc] initCircularRegionWithCenter:newLocation.coordinate radius:radius identifier:@"userLocation"];
[locationManager startMonitoringForRegion:reg desiredAccuracy:25];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
POI *poi = [[POI alloc] init];
[poi setCoordinate:newLocation.coordinate];
[poi setTitle:[df stringFromDate:[NSDate date]]];
[myMapView addAnnotation:poi];
MKCircle *circle = [MKCircle circleWithCenterCoordinate:newLocation.coordinate radius:radius];
[myMapView addOverlay:circle];
}
// Called when there is an error getting the location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{ NSLog(@"%@: %@", @"get location failed", error.description); }
#pragma mark MKMapViewDelegate methods
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKCircleView *circleView = [[MKCircleView alloc] initWithCircle:overlay];
circleView.fillColor = [UIColor blueColor];
circleView.alpha = 0.25;
return circleView;
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *annotationIdentifier = @"annotationIdentifier";
MKPinAnnotationView *newAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(newAnnotation == nil)
newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
[newAnnotation setAnimatesDrop:YES];
[newAnnotation setCanShowCallout:YES];
return newAnnotation;
}
@end
POI.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface POI : MKPointAnnotation
{
NSString *subtitle;
NSString *title;
NSInteger tag;
MKPinAnnotationColor pinColor;
}
@property(nonatomic,strong) NSString *subtitle;
@property(nonatomic,strong) NSString *title;
@property(nonatomic,assign) NSInteger tag;
@property(nonatomic,assign) MKPinAnnotationColor pinColor;
@end
POI.m
#import "POI.h"
@implementation POI
@synthesize tag, title, subtitle, pinColor;
@end