简体   繁体   English

iOS didEnterRegion从未调用

[英]iOS didEnterRegion never called

I have some problems with iOS and gefencing... 我在iOS和gefencing方面遇到一些问题...

//
//  ViewController.m
//  
//
//  Created by me on 14.05.13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<CLLocationManagerDelegate>

{
    CLLocationManager *locationManager;
    CLRegion *testRegion;
}
#define METERS_PER_MILE 1609.344
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initlocationManager];
//    [self startlocationManager];
}

// init location Manager Object, set delegation, result and accurate
- (void)initlocationManager {
    //create location manager object
    locationManager = [[CLLocationManager alloc]init];

    //this instance send its messages to our AppDelegate
    [locationManager setDelegate:self];

    //to get all results from the location manager
    [locationManager setDistanceFilter:kCLDistanceFilterNone];

    //be accurate as possible
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}

- (void)viewWillAppear:(BOOL)animated {

    CLLocationCoordinate2D startLocation;
    startLocation.latitude = +52.53753000;
    startLocation.longitude= +13.35971000;

    MKPointAnnotation *newLocation = [[MKPointAnnotation alloc] init];
    newLocation.coordinate = startLocation;
    newLocation.title = @"great title";
    newLocation.subtitle = @"great subtitle";
    MKCoordinateRegion startingRegion = MKCoordinateRegionMakeWithDistance(startLocation,                                                                              0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    testRegion = [[CLRegion alloc] initCircularRegionWithCenter:startLocation radius:500 identifier:@"TEST"];
    [_mapView addAnnotation:newLocation];
    [_mapView setRegion:startingRegion animated:YES];
    [locationManager startMonitoringForRegion:testRegion];
    NSLog(@"%@",locationManager.monitoredRegions);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Error : %@",error);
}

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    NSLog(@"Region monitoring failed with error: %@", [error localizedDescription]);
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
     NSLog(@"Entered Region - %@", region.identifier);
//    [self showRegionAlert:@"Entering Region" forRegion:testRegion.identifier];
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    NSLog(@"Started monitoring %@ region", region.identifier);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

The Terminal give me the following messages: 终端给我以下消息:

2013-05-15 00:24:47.739 [8712:13d03] {(
    (identifier TEST) <+52.53753000,+13.35972000> radius 500.00m
)}
2013-05-15 00:24:47.972 [8712:13d03] Started monitoring TEST region

So the region were successfully created and the monitoring start too but the didEnterRegion function never called. 因此成功创建了区域,并且监视也开始了,但是didEnterRegion函数从未调用过。

Help would be great! 帮助会很棒!

Edit: 编辑:

at the moment i'm using that gpx file in the iphone simulator and it works fine. 目前,我正在iPhone模拟器中使用该gpx文件,并且工作正常。 only didEnterRegion never get called. 只有didEnterRegion永远不会被调用。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx version="1.1" creator="http://www.geoplaner.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1"  xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
    <wpt lat="52.53856" lon="13.3515">
        <ele>36.6</ele>
        <name>WP04-D</name>
    </wpt>
    <wpt lat="52.53753" lon="13.35972">
        <ele>35.1</ele>
        <name>WP05-E</name>
    </wpt>
    <wpt lat="52.538" lon="13.35788">
        <ele>34.1</ele>
        <name>WP06-F</name>
    </wpt>
    <wpt lat="52.53844" lon="13.35633">
        <ele>33.6</ele>
        <name>WP07-G</name>
    </wpt>
    <wpt lat="52.53895" lon="13.35392">
        <ele>34.7</ele>
        <name>WP08-H</name>
    </wpt>
    <wpt lat="52.53813" lon="13.34925">
        <ele>36</ele>
        <name>WP09-I</name>
    </wpt>
    <wpt lat="52.53794" lon="13.34667">
        <ele>36.3</ele>
        <name>WP10-J</name>
    </wpt>
    <wpt lat="52.53815" lon="13.34504">
        <ele>37.1</ele>
        <name>WP11-K</name>
    </wpt>
    <wpt lat="52.5369" lon="13.35938">
        <ele>39.3</ele>
        <name>WP12-L</name>
    </wpt>
    <wpt lat="52.53619" lon="13.35792">
        <ele>33.3</ele>
        <name>WP13-M</name>
    </wpt>
    <wpt lat="52.53468" lon="13.35508">
        <ele>37.3</ele>
        <name>WP14-N</name>
    </wpt>
    <wpt lat="52.53398" lon="13.35367">
        <ele>37</ele>
        <name>WP15-O</name>
    </wpt>
    <wpt lat="52.53781" lon="13.35862">
        <ele>33.7</ele>
        <name>WP16-P</name>
    </wpt>

</gpx>

Your code looks fine. 您的代码看起来不错。 Just ensure that locationManager is retained/strongly-held. 只需确保locationManager被保留/牢固保留即可。

Test your region code using the iPhone simulator. 使用iPhone模拟器测试您的区域代码。 On the 'debug' menu chose a location from the 'location' submenu. 在“调试”菜单上,从“位置”子菜单中选择一个位置。 Changing different locations will exercise your code. 更改不同的位置将执行您的代码。

I just tested your code using the iPhone Simulator and by choosing locations from the submenu. 我只是使用iPhone模拟器并从子菜单中选择位置来测试您的代码。 I entered your code's lat/long numbers for 'custom location'. 我为“自定义位置”输入了您的代码的经纬度数字。 Here was the output: 这是输出:

2013-05-15 07:42:50.885 Location[69277:c07] {(
    (identifier home) <+37.26917400,-119.30660700> radius 100.00m
)}
2013-05-15 07:42:50.932 Location[69277:c07] Started monitoring TEST region
2013-05-15 07:44:01.103 Location[69277:c07] Entered Region - TEST
2013-05-15 07:44:59.573 Location[69277:c07] Entered Region - TEST

The 'entered region' output occurred when I chose the 'custom location'. 当我选择“自定义位置”时,发生“输入区域”输出。 The only changes I made from your code were: 1) commented out the 'mapView' lines and 2) declared the ViewController interface as shown below. 我对您的代码所做的唯一更改是:1)注释掉“ mapView”行,2)声明了ViewController接口,如下所示。 You had used a class extension. 您使用了类扩展。

@interface ViewController : UIViewController
  <CLLocationManagerDelegate> 
{
  CLLocationManager *locationManager;
  CLRegion *testRegion;
}
@end

Ensure that location services are enabled for your app. 确保为您的应用启用了位置服务。

Make sure you set the delegate. 确保设置了委托。 And you can simulate a move by using the gpx file. 您可以使用gpx文件模拟移动。 Include new file ->Resource->GPX File. 包括新文件->资源-> GPX文件。 Then just edit the file like below: 然后只需编辑文件,如下所示:

<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode"> 
    <wpt lat="LAT1" lon="LONG1">
         <name>Location</name>
    </wpt>
    <wpt lat="LAT2" lon="LONG2">
        <name>Location</name>
    </wpt>
    <wpt lat="LAT3" lon="LONG3">
        <name>Location</name>
    </wpt>
    <wpt lat="LAT4" lon="LONG4">
        <name>Location</name>
    </wpt>
    <wpt lat="LAT5" lon="LONG5">
        <name>Location</name>
    </wpt>
    <wpt lat="LAT6" lon="LONG6">
        <name>Location</name>
    </wpt>
    <wpt lat="LAT7" lon="LONG7">
        <name>Location</name>
    </wpt>
    <wpt lat="LAT8" lon="LONG8">
        <name>Location</name>
    </wpt>
    <wpt lat="LAT9" lon="LONG9">
        <name>Cupertino</name>
    </wpt>
    <wpt lat="LAT10" lon="LONG10">
        <name>Location</name>
    </wpt>

    <wpt lat="LAT11" lon="LONG11">
        <name>Location</name>
    </wpt>

</gpx>

Try to include the coordinates of inside and outside of region 尝试包括区域内部和外部的坐标

Now you can add this file in iphone simulator Debug->location->Custom location 现在您可以在iPhone模拟器中的Debug-> location-> Custom location中添加此文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM