2015-05-19 53 views
0

我使用iOS8上,並使用下面的代碼用於獲取經度和緯度通過GPS獲得在iOS8上的經度和緯度:無法通過GPS

-(void)CurrentLocationIdentifier 
{ 
    locationManager = [CLLocationManager new]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 && 
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) 
    //[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) 
    { 
    // Will open an confirm dialog to get user's approval 
    [locationManager requestWhenInUseAuthorization]; 
    //[_locationManager requestAlwaysAuthorization]; 
    } else { 
    [locationManager startUpdatingLocation]; //Will update location immediately 
    } 
} 

- (void)locationManager:(CLLocationManager *)manager 
didUpdateLocations:(NSArray *)locations { 
    CLLocation *location = [locations lastObject]; 
    NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude); 
} 

- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 
    switch (status) { 
    case kCLAuthorizationStatusNotDetermined: { 
     NSLog(@"User still thinking.."); 
    } 
    case kCLAuthorizationStatusDenied: { 
     NSLog(@"User hates you"); 
    } break; 
    case kCLAuthorizationStatusAuthorizedWhenInUse: 
    case kCLAuthorizationStatusAuthorizedAlways: { 
     [locationManager startUpdatingLocation]; //Will update location immediately 
    } break; 
    default: 
     break; 
} 
} 

運行此代碼我收到輸出的日誌後:用戶還在想。任何人都可以幫我解決這個問題我是iOS和CoreLocation.Framework的新手? 請幫我找到錯誤,我該如何解決這個問題。

+0

你是在模擬器還是真實設備上檢查這個? –

+0

@TapasPal我正在模擬器上檢查它,並且已經完成了設置,如調試>模擬位置>選項和模擬器調試>位置>蘋果。我跟着這個教程http://www.appcoda.com/how-to-get-current-location-iphone-user/ – Sushrita

回答

1

在InfoPlist.strings文件添加此字符串

1) NSLocationWhenInUseUsageDescription 

2) NSLocationAlwaysUsageDescription 

試試這個代碼

locationManagerApp=[[CLLocationManager alloc] init]; 

    locationManagerApp.delegate = self; 
    locationManagerApp.distanceFilter = kCLDistanceFilterNone; 
    locationManagerApp.desiredAccuracy = kCLLocationAccuracyHundredMeters; 

    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
    {   
      [locationManagerApp requestAlwaysAuthorization]; 
    } 

    [locationManagerApp startUpdatingLocation]; 
    CLLocation *location1 = [locationManagerApp location]; 
    CLLocationCoordinate2D coordinate = [location1 coordinate]; 
    self.latValue= [NSString stringWithFormat:@"%f", coordinate.latitude]; 
    self.longValue = [NSString stringWithFormat:@"%f", coordinate.longitude]; 
    NSLog(@"Latitude = %@", self.latValue); 
    NSLog(@"Longitude = %@", self.longValue); 

和運行項目中的設備。

+0

首先抱歉遲到的回覆,但我想運行它在模擬器中我已經完成了設置,如調試>模擬位置>選項和模擬器調試>位置>蘋果。你能提出任何關於這個的建議嗎 – Sushrita

+1

謝謝你的幫助和建議。我在infoPlist.strings文件中添加了字符串,它工作。 – Sushrita

1

在iOS 8中,您需要做兩件額外的事情才能獲得位置信息:將一個密鑰添加到Info.plist中,並請求位置管理器授權它開始。有兩個Info.plist密鑰用於新的位置授權。其中一個或兩個鍵都是必需的。如果這兩個鍵都不存在,則可以調用startUpdatingLocation,但位置管理器實際上不會啓動。它不會向委託發送失敗消息(因爲它從未開始,它不會失敗)。如果您添加一個或兩個密鑰,但忘記明確請求授權,它也會失敗。

所以,你需要做的第一件事就是添加下列鍵的一個或兩個的Info.plist文件:

NSLocationWhenInUseUsageDescription 
NSLocationAlwaysUsageDescription 

這兩個鍵的一個字符串這是爲什麼你的描述需要位置服務。您可以輸入一個字符串,如「需要查找您所在的位置」,如iOS 7中那樣,可以在InfoPlist.strings文件中進行本地化。

+0

謝謝你的工作 – Sushrita