通知用户每50/100米移动一次iOS

Cin*_*ntu 1 cocoa-touch cllocationmanager ios cllocationdistance

嗨,我想在位置变化50米(不少于此)时呼叫网络服务。我已尝试使用重大更改,但它至少可以运行500米,并且startupdatelocations始终会调用。因此,我如何检测设备是否从该位置移动了50或100米。

在许多stackoverflow问题中,我都使用距离过滤器为50米。但是在移动到50米之前我无法获得设备中的位置更新信息。

这里有一些关于距离过滤器的解释-iPhone的核心位置:距离过滤器如何工作?

小智 5

非常简单。

首先在您的ViewDidload方法中写入以分配CLLocationManager。

在这里我设置50M距离。

locationManager = [[CLLocationManager alloc] init];

    //there will be a warning from this line of code
    [locationManager setDelegate:self];

    //and we want it to be as accurate as possible
    //regardless of how much time/power it takes
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    //set the amount of metres travelled before location update is made
    [locationManager setDistanceFilter:50];
    [locationManager requestAlwaysAuthorization];
    [locationManager requestWhenInUseAuthorization];
    [locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)

因此,每隔50米的更换设备此方法称为:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    if (locations.count > 0) {
        CLLocation *location = locations.lastObject;
        User_latitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
        User_longitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
        NSLog(@"latitude = %f",location.coordinate.latitude);
        NSLog(@"longitude = %f",location.coordinate.longitude);
        [self webservice_UpdateLocation];
    }
}
Run Code Online (Sandbox Code Playgroud)