iOS:特定地理位置区域的警报用户(lat,long)

时间:2013-04-06 11:17:24

标签: ios geolocation cllocationmanager cllocation clregion

我正在尝试为用户实现Geo location的功能,我正在静态设置latitudelongitude信息,当应用启动时,如果用户位于其中我正在显示一条消息“你已经到达办公室”,否则“你要离开办公室”。我已经实现了以下代码来实现这一点,我尝试通过步骤和车辆四处移动,但在这两种情况下它总是显示“你已经到达办公室”,但是我距离那个位置2公里!我认为问题在于CLLocationManager委托中的地理数据的比较。

- (void) startUpdateUserLocation
{
    if(!locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

//    [locationManager startUpdatingLocation];

    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(latitude, longitude);

    CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coord radius:kCLDistanceFilterNone identifier:@"identifier"];
    [locationManager startMonitoringForRegion:region];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    latitude = 23.076289;
    longitude = 72.508129;
}

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    MKCoordinateRegion region;
    region.center = mapView.userLocation.coordinate;
    region.span = MKCoordinateSpanMake(0.25, 0.25);
    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:YES];

    lblCurrentCoords.text = [NSString stringWithFormat:@"lat %f lon %f",mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];

    [self startUpdateUserLocation];
}

- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
    [listOfPoints addObject:manager.location];
    [tablePoints reloadData];

    /*
     *  locationManager:didEnterRegion:
     *
     *  Discussion:
     *    Invoked when the user enters a monitored region.  This callback will be invoked for every allocated
     *    CLLocationManager instance with a non-nil delegate that implements this method.
     */

    lblLocationStatus.text = @"You're in office area!...";
}

- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
    /*
     *  locationManager:didExitRegion:
     *
     *  Discussion:
     *    Invoked when the user exits a monitored region.  This callback will be invoked for every allocated
     *    CLLocationManager instance with a non-nil delegate that implements this method.
     */

    lblLocationStatus.text = @"You're going out from office area!...";
}

- (void)locationManager:(CLLocationManager *)manager
didStartMonitoringForRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_TBD,__IPHONE_5_0)
{
    /*
     *  locationManager:didStartMonitoringForRegion:
     *
     *  Discussion:
     *    Invoked when a monitoring for a region started successfully.
     */

    lblLocationStatus.text = @"Start monitoring...";
}

- (void)locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
    /*
     *  locationManager:monitoringDidFailForRegion:withError:
     *
     *  Discussion:
     *    Invoked when a region monitoring error has occurred. Error types are defined in "CLError.h".
     */

    lblLocationStatus.text = @"Stop monitoring...";
}

我正在努力完成以下事情!

  1. 如果用户进入地理位置,他应该“警惕”。 ---如何匹配位置?
  2. 如果在Geo位置内移动,则代码应该监视此活动! ---需要设置所需的精度属性吗?
  3. 我希望我的代码能够不断检查用户的Geo位置,我该怎么做? ---需要在NSTimer
  4. 中调用函数

    我在SO上发现了很多关于同样问题的问题,但是没有一个问题符合答案!有人请指导我是否朝着正确的方向前进,因为这段代码没有出现! :)

1 个答案:

答案 0 :(得分:3)

听起来你应该使用区域监控,它会告诉你用户何时进入或退出圆形区域。使用startMonitoringForRegion:进行设置并实施CLLocationManagerDelegate方法

– locationManager:didEnterRegion:
– locationManager:didExitRegion:
– locationManager:monitoringDidFailForRegion:withError:
– locationManager:didStartMonitoringForRegion:

如果您在收到错误的位置数据时遇到问题,请检查CLLocationlocationManager:didUpdateLocations:locationManager:didUpdateToLocation:fromLocation:的年龄。如果它超过60秒,请不要使用它。