地理围栏不起作用

GeoFencing doesn't work

我正在制作一个带有 GeoFencing 的应用程序。我已阅读 here 并构建了一个应用程序。它不会崩溃,但一旦我进入监控区域,它也不会做出反应。它确实会注销 "Started monitoring" 但是一旦我 select 我的 *.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 区域时它没有任何反应?另外,如何创建更合适的标识符?

谢谢! 埃里克

我相信你忘记了将 locationManager 委托设置为你的视图控制器:

    locationManager.delegate = self;

创建 locationManager 后将其放入您的 viewDidLoad 中。

您还应该实施 monitoringDidFailForRegion 以检测任何错误。调用 startMonitoring 并不能保证它会实际启动。它可能因各种原因而失败。