我在iPhone模拟器中运行我的代码,从模拟器菜单中选择: “Debug,Location,Apple”将我当前的位置设置在Apple的Cupertino位置。
然后我从模拟器菜单中选择:“Debug,Location,Freeway Drive”。当前位置沿280高速公路正确更新。
我的代码应该创建一个1500米的“地理围栏”。每次设备到达边界时 - 它会掉落一个引脚并画一个圆圈。
问题.....
任何帮助表示赞赏!!谢谢!
这是我的代码:
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