GPS位置的本地推送通知

时间:2014-03-11 10:22:36

标签: ios gps push-notification core-location

我有一个关于iOS,GPS定位和推送通知的简单问题。

当设备接近特定的GPS位置时,iOS中是否可以发送本地推送通知?

2 个答案:

答案 0 :(得分:3)

以下ViewController.h文件的代码

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>{
    CLLocationManager *locationManager;
    NSString* lastNotification;
}

@end

以下ViewController.m文件的代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    lastNotification = @"";
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -User Actions
- (IBAction)startLocaingMe:(id)sender{
    [self startLocationReporting];
}

#pragma mark - Location Metods

- (void)startLocationReporting {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;//or whatever class you have for managing location
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // If it's a relatively recent event, turn off updates to save power
    CLLocation* location = [locations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0) {
        [self showNotificationIfDistanceIs100:location];
    }
}

// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
}

- (CLLocationDistance)distanceBetweenTwoPoints:(CLLocation*) location1 andSecond:(CLLocation*) location2{
    CLLocationDistance distance = [location1 distanceFromLocation:location2];
    return distance;
}

-(CLLocation*)House{
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.030064 longitude:72.546193];
    return loc;
}

-(CLLocation*)passportOffice{
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.032034 longitude:72.549999];
    return loc;
}

-(CLLocation*)ldCollageBusStand{
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:23.032515 longitude:72.549307];
    return loc;
}

-(void)showNotificationIfDistanceIs100:(CLLocation*) location{
    if ([self distanceBetweenTwoPoints:location andSecond:[self House]] <= 100) {
        [self setLocalNotificaion:@"Your are 100 meter form House"];
    }else if ([self distanceBetweenTwoPoints:location andSecond:[self passportOffice]] <= 100){
        [self setLocalNotificaion:@"Your are 100 meter form Passport Office"];
    }else if ([self distanceBetweenTwoPoints:location andSecond:[self ldCollageBusStand]] <= 100){
        [self setLocalNotificaion:@"Your are 100 meter form Ld Collage Bus Stand"];
    }

}

-(void)setLocalNotificaion:(NSString*)msg{
    if ([lastNotification isEqualToString:msg] != YES) {
        lastNotification = msg;
        UILocalNotification *futureAlert;
        futureAlert = [[UILocalNotification alloc] init];
        [futureAlert setAlertBody:msg];
        futureAlert.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
        futureAlert.timeZone = [NSTimeZone defaultTimeZone];
        [[UIApplication sharedApplication] scheduleLocalNotification:futureAlert];
    }
}

@end

您可以更改位置Long / Lati并获取Localnotification

这可能很有帮助

<强> ------- EDITED ---------

enter image description here

答案 1 :(得分:0)

您可以实施LocationManager实例,并通过以下方法获得有关位置更新的通知:

- (void)locationManager:(CLLocationManager *)manager
      didUpdateLocations:(NSArray *)locations {
   // CLLocation object will be the content of the array locations
}

您可以根据自己的要求发布UILocalNotification