Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 位置更新问题_Ios_Objective C_Cllocationmanager_Cllocation - Fatal编程技术网

Ios 位置更新问题

Ios 位置更新问题,ios,objective-c,cllocationmanager,cllocation,Ios,Objective C,Cllocationmanager,Cllocation,我知道有很多问题与此相关,但我无法与我的问题联系起来。在我的应用程序中,我正在获取附近的餐厅,最初,如果用户单击“附近”按钮,我将使用下面的代码获取横向、纵向和位置标记详细信息 -(void)setUpUserLocation { BOOL locationAllowed = [CLLocationManager locationServicesEnabled]; if (locationAllowed==NO) { UIAlertView *alertVi

我知道有很多问题与此相关,但我无法与我的问题联系起来。在我的应用程序中,我正在获取附近的餐厅,最初,如果用户单击“附近”按钮,我将使用下面的代码获取横向、纵向和位置标记详细信息

-(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;
}

我的问题是,如果用户位置发生变化,我如何向用户发出通知,如
您的位置发生变化,您需要更新它,然后只有您可以在附近的餐厅
位置弹出窗口应该在用户位置发生变化时出现
,否则不要每次都调用
didUpdateLocation
。我调用了
startMonitoringSignificantLocationChanges
,但它没有在第一次调用didUpdateLocation本身。关于这方面的任何帮助。

在一段时间后检查,或者在用户移动后检查(无论您想要什么),如果条件为真,请调用服务

我认为这对你有帮助

步骤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
       }
    }

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

我认为这对你有帮助

步骤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
       }
    }

对不起,我不完全理解这个问题。您是否希望向用户显示一个警报,通知他们位置已更改,因此他们需要更新应用程序以查找附近的餐厅?如果用户的位置发生变化,您可以重新计算餐厅的位置。现在,如果您确实需要这样做,那么只需在didUpdateLocation上以对您的案例有意义的准确性进行操作。如果您使用SignificantLocationChanges,则位置管理器仅会在位置发生重大变化(通常为几百米)时通知您。谢谢,如果用户的位置发生变化,则再次通知您,我需要一些弹出窗口来向用户显示您的位置随
allow
not allow
按钮的变化。如果他们按了allow,则更新新位置。你还提到了
显著的位置变化
你能帮我解决这个问题吗?我不知道。locationManager.desiredAccuracy=KCallocationAccuracyHundredMeters;locationManager.distanceFilter=100;有没有代表可以找到,用户在该位置的百米范围内,或者他们到达了…我可以根据这一点调用Web服务,这对我很有帮助看看这里你可以找到有关iOS如何处理位置变化的信息,您可以通过调用startMonitoringSignificantLocationChanges来使用重要的位置更改,我认为您已经尝试过了。关于警报,您可以使用UIAlertController。如果发生重大位置更改,您的位置管理器将调用您的代理的didUpdateLocation方法,您应该已经准备好了我所了解的方法。抱歉,我不完全理解这个问题。您是否希望向用户显示一个警报,通知他们位置已更改,因此他们需要更新应用程序以查找附近的餐厅?如果用户的位置发生变化,您可以重新计算餐厅的位置。现在,如果您确实需要这样做,那么只需在didUpdateLocation上以对您的案例有意义的准确性进行操作。如果您使用SignificantLocationChanges,则位置管理器仅会在位置发生重大变化(通常为几百米)时通知您。谢谢,如果用户的位置发生变化,则再次通知您,我需要一些弹出窗口来向用户显示您的位置随
allow
not allow
按钮的变化。如果他们按了allow,则更新新位置。你还提到了
显著的位置变化
你能帮我解决这个问题吗?我不知道。locationManager.desiredAccuracy=KCallocationAccuracyHundredMeters;locationManager.distanceFilter=100;有没有代表可以找到,用户在该位置的百米范围内,或者他们到达了…我可以根据这一点调用Web服务,这对我很有帮助看看这里你可以找到有关iOS如何处理位置变化的信息,您可以通过调用startMonitoringSignificantLocationChanges来使用重要的位置更改,我认为您已经尝试过了。关于警报,您可以使用UIAlertController。如果发生重大位置更改,您的位置管理器将调用代理的didUpdateLocation方法,据我所知,您应该已经准备好了。