当用户每移动50/100米iOS时通知

时间:2015-11-26 05:18:46

标签: ios cocoa-touch cllocationmanager cllocationdistance

嗨我想在地点变化50米(不小于此值)时拨打网络服务。我尝试过使用重大更改,但它最少工作500米,并且startupdatelocations将一直调用。那么如何检测设备是否从该位置移动了50或100米。

我已经使用距离过滤器50米,如许多stackoverflow问题所述。但是在移动到50米之前它没有工作我在设备中获得了位置更新。

这里有人解释了距离过滤器 - iphone core location: distance filter how does it work?

1 个答案:

答案 0 :(得分:4)

很容易。

首先在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];

所以每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];
    }
}