在一定距离后获取位置更新(在我的情况下为5米)

时间:2016-05-31 11:30:11

标签: ios objective-c gps core-location

我正在从用户那里输入距离滤波器。出于测试目的,我认为它是5米。但走了5米后,我没有获得位置更新。我在iPhone模拟器5s上测试它。

MainScreenController.m

@interface MainScreenController ()

@property (strong, nonatomic) IBOutlet UITextField *txtTakeDistance;
-(void)writeToTextFile : (NSArray<CLLocation *> *) location;
-(void)ShowContentlist;
@end

@implementation MainScreenController
{
    //CLLocation *location;
    CLLocationManager *locationManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    locationManager = [[CLLocationManager alloc]init];


}
- (IBAction)btnSetDistance:(id)sender {

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; // Tried every accuracy option
    locationManager.pausesLocationUpdatesAutomatically = NO;
    locationManager.distanceFilter = [_txtTakeDistance.text doubleValue];

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


}



- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

 NSLog(@"didFailWithError: %@", error);
 UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Error" message:@"Failed to fecth current location" preferredStyle:UIAlertControllerStyleAlert];
 UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

 }];
 [controller addAction:okAction];
 [self presentViewController:controller animated:YES completion:nil];
 }


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

 CLLocation *currentLoaction;
 currentLoaction = [locations lastObject];

     if(locations != nil){

     NSLog(@"--> : %@",locations);

     [self writeToTextFile:locations];
 }
     [locationManager stopUpdatingLocation];

 }

-(void)writeToTextFile : (NSArray<CLLocation *> *) location{

    NSFileManager *fileManager = [NSFileManager defaultManager];
    CLLocation *currentLocation;
    currentLocation = [location lastObject];
    NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
    [timeFormat setDateFormat:@"yyyy-MM-DD HH:mm:ss"];

    NSString *timeStamp = [timeFormat stringFromDate:currentLocation.timestamp];
    NSString *content = [NSString stringWithFormat:@"\n%f|%f|%@\n",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,timeStamp];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [paths objectAtIndex:0];

    if ([fileManager fileExistsAtPath:documentDir]==YES) {

        NSString *documentFile = [documentDir stringByAppendingPathComponent:@"log.txt"];
        NSString *textFromFile = [NSString stringWithContentsOfFile:documentFile encoding:NSUTF8StringEncoding error:nil];
        NSString *textToFile = [textFromFile stringByAppendingString:content];
        [textToFile writeToFile:documentFile atomically:YES encoding:NSUTF8StringEncoding error:nil];

    }else{

        NSLog(@"No such file");
    }
}

如果我以错误的方式做错,请纠正我。所有建议都表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:0)

对您的代码进行一些更改,例如:

- (IBAction)btnSetDistance:(id)sender {

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;

locationManager.distanceFilter =5;

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

最重要的是,你无法通过移动mac来检查移动的距离。

你应该在你的设备上查看它。

答案 1 :(得分:0)

试试此代码

- (IBAction)btnSetDistance:(id)sender
{
    locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    if (([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]))
    {
        [locationManager requestWhenInUseAuthorization];
    }
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = 5;   // change it according to need
    [locationManager startUpdatingLocation];

}

如果您覆盖地图,则每隔5米距离此行locationManager.distanceFilter = 5;将更新

要获取更新的纬度和经度,请使用此代码

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation* location = [locations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

    double  lat = location.coordinate.latitude;
    double  longi = location.coordinate.longitude;

    if (abs(howRecent) < 15.0)

    {
        NSLog(@"%f,%f",lat,longi);
        [locationManager stopUpdatingLocation];
    }
}