GeoFencing не работает

Я делаю приложение с GeoFencing. Я прочитал здесь и создал приложение. Он не падает, но и не реагирует, когда я вхожу в контролируемую область. Он выдает "Запущенный мониторинг", но не реагирует, когда я выбираю свой файл *.GPX. Это мой код ViewController:

@interface myViewController () {
CLLocationManager *locationManager;
}

@end

@implementation RegisterViewController

@synthesize GeoFences;

- (void)viewDidLoad {
[super viewDidLoad];

// Initalize locationManager
locationManager = [[CLLocationManager alloc] init];
}

- (IBAction)getLocation:(id)sender {
// Start monitoring
// Create a home NSDictionary
NSDictionary *myDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"23123124arevar", @"title", @"00.0", @"latitude", @"00.0", @"longitude", @"100", @"radius", nil];
NSMutableArray *regions = [[NSMutableArray alloc] init];
CLRegion *region = [self mapDictionaryToRegion:myDictionary];
[regions insertObject:region atIndex:0];
NSLog(@"Count: %lu", [regions count]);
[self startMonitoringRegions:regions];
}

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

- (CLRegion*)mapDictionaryToRegion:(NSDictionary*)dictionary
{
NSString *title = [dictionary valueForKey:@"title"];

CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                               radius:regionRadius
                                           identifier:title];
}

-(void)startMonitoringRegions:(NSMutableArray *)array {
for (CLRegion *GeoFence in array)
{
    NSLog(@"Started monitoring");
    [locationManager startMonitoringForRegion:GeoFence];
}
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"Exited Region - %@", region.identifier);
// post a notification
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"Exited Region - %@", region.identifier);
// post a notification
}

@end

Вот мой файл *.GPX:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<gpx
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
version="1.1" 
creator="gpx-poi.com">
<wpt lat="00.0" lon="00.0">
<time>2015-01-01T14:45:02Z</time>
</wpt>
</gpx>

Я заменил мои фактические координаты на 00.0.

Может ли кто-нибудь помочь мне с этим, и почему он ничего не делает, когда я захожу в свою область *.GPX? Кроме того, как я могу создать более подходящий идентификатор?

Спасибо! Erik

1 ответ

Решение

Я полагаю, что вы забыли установить делегат locationManager на свой контроллер представления:

    locationManager.delegate = self;

Поместите это в viewDidLoad после создания locationManager.

Вы должны также реализовать monitorDidFailForRegion для обнаружения любых ошибок. Вызов startMonitoring не гарантирует, что он действительно начнется. Это может потерпеть неудачу по разным причинам.

Другие вопросы по тегам