当iphone到达某个位置时如何触发警报?
Posted
技术标签:
【中文标题】当iphone到达某个位置时如何触发警报?【英文标题】:How to trigger alert when iphone reaches a certain location? 【发布时间】:2012-07-22 00:34:43 【问题描述】:我目前有一张显示用户当前位置的地图,还有几个注释标记(我还没有将它们放入一个数组中,但很快就会)。我试图让我的 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
查看控制器.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
【问题讨论】:
我假设您还没有阅读此内容? --> developer.apple.com/library/ios/#documentation/CoreLocation/… 滚动到“使用区域监控边界交叉点”部分 似乎您需要设置一个带有标记作为边界的地理围栏。请参阅以下堆栈溢出链接***.com/questions/8077691/… 【参考方案1】:您想寻找区域监控。查看-[CLLocationManager startMonitoringForRegion:]
的文档。
【讨论】:
以上是关于当iphone到达某个位置时如何触发警报?的主要内容,如果未能解决你的问题,请参考以下文章
Iphone presentLocalNotificationNow 在后台应用程序时不会触发警报和声音