当用户每移动 50/100 米时通知 iOS
Notify when the user moved every 50/100 meters iOS
嗨,我想在位置变化 50 米(不少于此)时调用网络服务。我尝试过使用重大更改,但它至少适用于 500 米,并且 startupdatelocations 会一直调用。那么我如何检测设备是否从该位置移动了 50 或 100 米。
我在许多 Whosebug 问题中都使用了 50 米的距离过滤器。但是在移动到 50 米之前它不起作用我在设备中获得了位置更新。
这里有人解释了距离过滤器 - iphone core location: distance filter how does it work?
非常简单。
首先在你的ViewDidload
方法中写入Alloc 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];
}
}
嗨,我想在位置变化 50 米(不少于此)时调用网络服务。我尝试过使用重大更改,但它至少适用于 500 米,并且 startupdatelocations 会一直调用。那么我如何检测设备是否从该位置移动了 50 或 100 米。
我在许多 Whosebug 问题中都使用了 50 米的距离过滤器。但是在移动到 50 米之前它不起作用我在设备中获得了位置更新。
这里有人解释了距离过滤器 - iphone core location: distance filter how does it work?
非常简单。
首先在你的ViewDidload
方法中写入Alloc 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];
}
}