2010-07-23 187 views
-3

我想讓我的應用程序獲取他每秒鐘的位置(經度和緯度),這有可能嗎?我有這樣的代碼,到目前爲止,每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]; 
} 
/* 
+3

NSLog「陰莖」真的很優雅。有沒有人閱讀文檔了? – Nick 2010-07-23 06:44:56

回答

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. 
} 

無需使用任何計時器。

+0

好的,但我需要知道每秒的確切位置,所以如果我每秒在街上跑步,它會改變位置? – hugo411 2010-07-23 07:13:39

+0

將distanceFilter和desiredAccuracy設置爲最低會給你最新的更新。但是沒有理由說你會每秒都會得到一個更新。當GPS設備檢測到它已經移動時,您只會獲得更新。 – 2010-07-23 08:52:54