位置更新问题iOS

时间:2016-06-04 08:17:22

标签: ios objective-c cllocationmanager cllocation

我知道有很多与此相关的问题,但我无法与我的问题联系。在我的应用程序中,我正在获取附近的餐馆,最初,如果用户点击“附近”按钮,我会获取lat,long,placemark详细信息,请使用下面的代码。

-(void)setUpUserLocation
{
    BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
   if (locationAllowed==NO)
    {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"No authorization"
        message:@"Please, enable access to your location"
        delegate:self cancelButtonTitle:@"Cancel"
        otherButtonTitles:@"Open Settings", nil];
        [alertView show];
    }
    else
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;

        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [locationManager requestWhenInUseAuthorization];
        }
    }
    [locationManager startUpdatingLocation];
}


-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location=[locations lastObject];
    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = placemarks[0];
        NSDictionary *addressDictionary = [placemark addressDictionary];

    }];
    [self stopSignificantChangesUpdates];

}
- (void)stopSignificantChangesUpdates
{
    [locationManager stopUpdatingLocation];
    locationManager = nil;
}

我的问题是,如果用户位置发生变化,我该如何向用户发出Your location changed, you need to update it then only you can get a Nearby Restaurant Location popup should come once the user location changes的提醒,否则我不想每次都致电didUpdateLocation。我打电话给startMonitoringSignificantLocationChanges然后它没有在第一次调用didUpdateLocation。对此有任何帮助。

1 个答案:

答案 0 :(得分:2)

一段时间后检查或在用户移动后检查(无论你想要什么),如果条件为真,则拨打服务...

我认为它会对你有帮助;

第1步

 (void)viewDidLoad {
[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];
//set the amount of metres travelled before location update is made
[locationManager setDistanceFilter:100];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

[locationManager requestAlwaysAuthorization]; 

// call the timer with 5 minutes cap using 5 * 60 = 300
[NSTimer scheduledTimerWithTimeInterval:300.0f target:self selector:@selector(sendlocation1) userInfo:nil repeats:YES];

第2步

每100米更换设备此方法称为:

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%f",newLocation.coordinate.latitude);

    NSLog(@"%f",newLocation.coordinate.longitude);
    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
         if(meters >=100)
      {
         // call webservice for location is updated 
         [self sendlocation1];
      }else
      {
       // call normal method
       }
    }