2012-12-24 36 views
0

我在我的應用程序delegate中調用locationManager,並且回頭參考各種viewControllersviewDidAppear方法中的getUserLatitude和getUserLongitude函數。viewDidAppear方法在locationManager之前調用可以找到用戶

我的問題是,我viewDidAppear方法太快了appDelegate定位用戶推出,而我得到0.000000的latitude和0.000000爲longitude。有誰知道我可以如何解決這個問題?謝謝!

我的appDelegate的結構與這些位置的方法:

- (NSString *)getUserCoordinates 
{ 
    NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f", 
    locationManager.location.coordinate.latitude, 
    locationManager.location.coordinate.longitude]; 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locationManager startUpdatingLocation]; 
    return userCoordinates; 
} 

- (NSString *)getUserLatitude 
{ 
    NSString *userLatitude = [NSString stringWithFormat:@"%f", 
    locationManager.location.coordinate.latitude]; 
    return userLatitude; 
} 

- (NSString *)getUserLongitude 
{ 
    NSString *userLongitude = [NSString stringWithFormat:@"%f", 
    locationManager.location.coordinate.longitude]; 
    return userLongitude; 
} 

我使用這段代碼來從的appDelegate更新的位置:

- (void)viewDidAppear:(BOOL)animated 
{ 
    NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
    getUserLatitude]; 

    NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
    getUserLongitude]; 
} 
+0

設置CLLocationManagerDelegate委託在你的委託和文件的.h文件按照我在我的答案中提到的。 – Girish

回答

0

我也是在我的一個應用中面臨同樣的問題。首先分配CLLocationManager,將其設置爲delegate,然後調用startUpdatingLocation方法,這可能會解決您的問題。

locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locationManager startUpdatingLocation]; 

設置委託後,它調用其委託的方法&給了我們當前位置作爲的lattitude & longitude

//delegate method 
- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{  
    self.currentLat=newLocation.coordinate.latitude; 
    self.currentLong=newLocation.coordinate.longitude; 
    NSLog(@"appDelegate newLocation %f %f",self.currentLat,self.currentLong); 
} 
+0

謝謝Girish!我檢查出來! – Brandon

1

位置管理器更新位置後,會調用一個委託,可以在此處使用它。在位置更新後,您將在didUpdateToLocation方法中獲得控件

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


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"Failed %d",[error code]); 

} 
+0

嗨,我已經嘗試在viewControllers中的viewDidAppear方法之上添加那些代碼,這些代碼引用了appDelegate。他們似乎從來沒有打過電話。你知道這可能是爲什麼嗎? – Brandon

+0

你寫了locationManager.delegate = self你聲明位置管理器 –

+0

我不這麼認爲。我會檢查它 – Brandon

0

我通過執行類似操作來解決此問題。

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 


    NSLog(@"Ahsan"); 
    if (!oldLocation || 
     (oldLocation.coordinate.latitude != newLocation.coordinate.latitude && 
     oldLocation.coordinate.longitude != newLocation.coordinate.longitude && 
     newLocation.horizontalAccuracy <= 100.0)) 
    { 
     //Your code comes here... 
    } 
} 
相關問題