Ios 地球围栏不起作用

Ios 地球围栏不起作用,ios,objective-c,iphone,clregion,regional,Ios,Objective C,Iphone,Clregion,Regional,我正在用地理围栏制作一个应用程序。我已经阅读并构建了一个应用程序。它不会崩溃,但一旦我进入监控区域,它也不会做出反应。它确实会注销,但在我选择*.GPX文件后不会做出反应。这是我的ViewController代码: @interface myViewController () { CLLocationManager *locationManager; } @end @implementation RegisterViewController @synthesize GeoFences;

我正在用地理围栏制作一个应用程序。我已经阅读并构建了一个应用程序。它不会崩溃,但一旦我进入监控区域,它也不会做出反应。它确实会注销,但在我选择*.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

我相信您忘记将locationManager委托设置到视图控制器:

    locationManager.delegate = self;
创建locationManager后,将其放入viewDidLoad


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

现在效果很好。只有几个问题,我看不到didFailToStartMonitoring方法?另外,如果应用程序没有运行,这些方法会运行吗?如果应用程序没有运行,并且我在didEnterRegion内创建了UIAlertView,会发生什么?对不起,我是从内存中获取的。该方法实际上是在监视DidFailForRegion。这是一个委托方法。如果您的应用程序在进入该区域时尚未运行,则将由iOS启动。但是,我现在记不起在这种情况下是否触发了didEnterRegion方法。我使用过它,如果您在任务管理器中关闭应用程序而不将其刷走,则当您进入/离开区域时,它仍会记录NSLog,但在打开应用程序之前不会创建任何UIAlertView或类似的内容。我没有检查是否通过滑动来关闭应用程序,因为这对模拟器来说有点困难——但我猜结果会是一样的?调试时我无法停止任务,我将在iPhone上尝试。但是,我如何检查用户是否在该区域内,例如使用按钮?我能把电话接到迪登特地区吗?还是更好的办法?