2012-07-22 34 views
3

我目前有一張地圖,顯示用戶的當前位置,也有一些註釋標記(我還沒有把它們放入數組,但很快)。我試圖讓我的iphone應用程序在用戶到達特定位置時觸發警報。我怎麼做?這就是我現在所擁有的......如何在iphone到達特定位置時觸發提醒?

ViewController.h

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 
#import <MapKit/MapKit.h> 
#import "MapAnnotation.h" 

@interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate> 
{ 
    CLLocationManager *locationManager; 
    IBOutlet MKMapView *worldView; 


} 
@end 

視圖Controller.m或者

#import "ViewController.h" 


@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [worldView setShowsUserLocation:YES];  

} 


- (void)mapView:(MKMapView *)mapView 
didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 

    CLLocationCoordinate2D loc = [userLocation coordinate]; 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250); 
    [worldView setRegion:region animated:YES]; 

    MKCoordinateRegion pin = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    pin.center.latitude = 53.363581; 
    pin.center.longitude = -6.258183; 
    pin.span.longitudeDelta = 0.01f; 
    pin.span.latitudeDelta = 0.01f; 
    [worldView setRegion:pin animated:YES]; 

    MapAnnotation *stage1 = [[MapAnnotation alloc] init]; 
    stage1.title = @"Quinn's"; 
    stage1.coordinate = pin.center; 
    [worldView addAnnotation:stage1]; 

    MKCoordinateRegion pin2 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    pin2.center.latitude = 53.364678; 
    pin2.center.longitude = -6.263009; 
    pin2.span.longitudeDelta = 0.01f; 
    pin2.span.latitudeDelta = 0.01f; 
    [worldView setRegion:pin2 animated:YES]; 

    MapAnnotation *stage2 = [[MapAnnotation alloc] init]; 
    stage2.title = @"Neighbour"; 
    stage2.coordinate = pin2.center; 
    [worldView addAnnotation:stage2]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 
     // Create location manager object 
     locationManager = [[CLLocationManager alloc] init]; 

     // Setting Delegate as AnotherMapFirstViewController 
     [locationManager setDelegate:self]; 

     // And we want it to be as accurate as possible 
     // regardless of how much time/power it takes 
     [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 

      self.title = NSLocalizedString(@"First", @"First"); 
      self.tabBarItem.image = [UIImage imageNamed:@"first"]; 


    } 

    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"%@", newLocation); 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{ 
    NSLog(@"Could not find location: %@", error); 
} 


- (void)dealloc 
{ 
    // Tell the location manager to stop sending us messages 
    [locationManager setDelegate:nil]; 
} 


@end 
+1

我假設你還沒有讀過這個呢? - > http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringForRegion:desiredAccuracy 滾動到「使用區域來監視邊界過境」部分 – 2012-07-22 00:41:01

+1

似乎您需要設置帶有標記作爲邊界的地理圍欄。請參閱以下堆棧溢出鏈接http://stackoverflow.com/questions/8077691/notification-from-background-geofencing- in-ios-5 – 2012-07-22 01:43:19

+0

Got it!謝謝! – skinnypinny 2012-07-24 16:37:52

回答

0

您想尋找區域監測。檢查文檔-[CLLocationManager startMonitoringForRegion:]

相關問題