如何从位置管理器获取当前位置?

时间:2010-07-23 06:41:08

标签: iphone localization location

我希望我的应用能够获得每秒的确切位置(经度和纬度),这是可能的吗?到目前为止我有这个代码,应用程序每3秒获取当前位置,但如果我移动它不会更新...

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSLog(@"lol");


    [locationManager stopUpdatingLocation];



        if (wtf == 2) {
            [[NSUserDefaults standardUserDefaults] setObject:newLocation forKey:@"old"];
            NSLog(@"wtf");
            wtf =0;


        }

    oldLocation = [[NSUserDefaults standardUserDefaults] objectForKey:@"old"];

        double rep = [oldLocation distanceFromLocation:newLocation];
        NSString *label1 = [NSString stringWithFormat:@"%2.90f",oldLocation.coordinate.latitude];
        NSString *label2 = [NSString stringWithFormat:@"%2.90f",newLocation.coordinate.latitude];

    if ([label1 isEqual:label2]) {
        NSLog(@"penis");
    }

    labelm.text = label1;
    labelkm.text = label2;


}



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

}

-(void)actualise{





    [locationManager startUpdatingLocation];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    wtf = 2;

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(actualise) userInfo:nil repeats:YES];
}
/*

1 个答案:

答案 0 :(得分:3)

使用此代码初始化并启动位置管理器:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];

并实现 didUpdateToLocation ,如下所示:

- (void) locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*) oldLocation 
{
   // This will be called every time the device has any new location information. 
}

无需使用任何计时器。