iOS:针对特定地理位置区域(纬度、经度)向用户发出警报

iOS:针对特定地理位置区域(纬度、经度)向用户发出警报,ios,geolocation,cllocationmanager,cllocation,clregion,Ios,Geolocation,Cllocationmanager,Cllocation,Clregion,我试图为用户实现一个地理位置功能,我静态设置纬度和经度信息,当应用程序启动时,如果用户在该区域内,我会显示一条消息“您已到达办公室”或者“您正在离开办公室”。我已经实现了下面的代码来实现这一点,我试着通过台阶和车辆四处移动,但在这两种情况下,它总是显示“您已到达办公室”,然而我离那个位置有2公里远!我认为问题在于CLLocationManagerdelegate中的地理数据比较 - (void) startUpdateUserLocation { if(!locationManager)

我试图为用户实现一个
地理位置
功能,我静态设置
纬度
经度
信息,当应用程序启动时,如果用户在该区域内,我会显示一条消息“您已到达办公室”或者“您正在离开办公室”。我已经实现了下面的代码来实现这一点,我试着通过台阶和车辆四处移动,但在这两种情况下,它总是显示“您已到达办公室”,然而我离那个位置有2公里远!我认为问题在于
CLLocationManager
delegate中的地理数据比较

- (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...";
}
我正在努力完成以下事情

  • 如果用户输入地理位置,他应该“警惕”——如何匹配位置
  • 如果在该地理位置内移动,则代码应监视此活动需要设置所需的精度属性吗
  • 我想让我的代码不断检查用户的地理位置,我该怎么做?--需要在
    NSTimer
    中调用函数吗

  • 我在网上发现了很多问题,所以问的都是同一个问题,但没有人给出匹配的答案!有人请指导我是否朝着正确的方向前进,因为这个代码没有显示出来!:)

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

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

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

    没问题。正确设置区域监视可能很棘手。查看Xcode中“区域”的示例代码。如果您查看文档查看器,可以通过搜索找到示例代码。快速看一眼,您所做的看起来不错,但是半径:kCLDistanceFilterNone应该使用实际的半径数字,比如100(100米)。