【问题标题】:GeoFencing doesn't work地理围栏不起作用
【发布时间】:2015-01-01 17:03:14
【问题描述】:

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

谢谢! 埃里克

【问题讨论】:

    标签: ios objective-c iphone clregion regional


    【解决方案1】:

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

        locationManager.delegate = self;
    

    在创建 locationManager 后把它放在你的 viewDidLoad 中。

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

    【讨论】:

    • 现在效果很好。就几个问题,看不到didFailToStartMonitoring方法?此外,如果应用程序未运行,这些方法是否会运行 - 如果应用程序未运行并且我在 didEnterRegion 中创建了 UIAlertView 会发生什么?
    • 对不起,我是从记忆中开始的。该方法实际上是monitoringDidFailForRegion。这是一个委托方法。如果您的应用在您进入该区域时尚未运行,则您的应用将由 iOS 启动。但是,我现在不记得在这种情况下是否触发了 didEnterRegion 方法。
    • 我玩过它,如果你关闭应用程序而不在任务管理器中滑动它,当你进入/离开一个区域时它仍然会 NSLog,但不会创建任何 UIAlertViews 或类似的应用程序已打开。我没有通过滑动来检查关闭应用程序,因为这对模拟器来说有点困难 - 但我想结果会是一样的吗?调试时我无法停止任务,我会在我的 iPhone 上尝试一下。但是,我如何使用例如按钮检查用户是否在该区域内?我可以将 BOOL 连接到 didEnterRegion 还是更好的方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2015-06-22
    相关资源
    最近更新 更多