2015-06-08 40 views
0

我一直試圖獲取當前位置IOS8和跟隨線程Location Services not working in iOS 8,但獲取00.00,00.00而不是當前位置。任何關於我在哪裏做錯的建議。IOS CLLocationManager正在返回緯度:0.000000經度:0.000000

ViewController.h

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 

@interface ViewController : UIViewController <CLLocationManagerDelegate> 

@property (strong, nonatomic) CLLocationManager *locationManager; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.distanceFilter = kCLDistanceFilterNone; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[self.locationManager startUpdatingLocation]; 
NSLog(@"%@", [self deviceLocation]); 


CLLocation *location = [self.locationManager location]; 

// Configure the new event with information from the location 
CLLocationCoordinate2D coordinate = [location coordinate]; 

NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude]; 


NSLog(@"%@",latitude); 
NSLog(@"%@",longitude); 

[super viewDidLoad]; 

}

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 

}

-(void)setUpMap 
{ 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 
#ifdef __IPHONE_8_0 
if(IS_OS_8_OR_LATER) { 
    // Use one or the other, not both. Depending on what you put in info.plist 
    [self.locationManager requestAlwaysAuthorization]; 
} 
#endif 
[self.locationManager startUpdatingLocation]; 

} 



-(NSString *)deviceLocation 
{ 
    return [NSString stringWithFormat:@"latitude: %f longitude: %f",   self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude]; 
} 
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    [manager stopUpdatingLocation]; 

CLLocationCoordinate2D coordinate_currentlocation = [newLocation coordinate]; 

float latitude_current = newLocation.coordinate.latitude; 
float longitude_current = newLocation.coordinate.longitude; 


NSLog(@"%f",latitude_current); 
NSLog(@"%f",longitude_current); 

NSLog(@"Current latitude :%f",coordinate_currentlocation.latitude); 
NSLog(@"Current longitude :%f",coordinate_currentlocation.longitude); 
} 
@end 

我還增加了以下關鍵在我的info.plist

<key>NSLocationAlwaysUsageDescription</key> 
<string>Your location is needed for this app.</string> 

回答

0

添加此字符串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); 

和運行項目設備。如果你在模擬器中運行項目,那麼不會獲得緯度/經度。

獲取模擬器上的當前位置(選擇任意一個位置)。 enter image description here

+0

忘了提及我在設備中獲取座標,但想要測試一下其他工作流程,一旦我在模擬器上獲得座標。 – Santosh

0

代碼有錯誤,實際檢查授權的函數從未調用過。我將所有代碼移到了viewDidLoad中,如下所示。感謝您關注此事。

- (void)viewDidLoad { 

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

#ifdef __IPHONE_8_0 
     if(IS_OS_8_OR_LATER) 
     { 
      [self.locationManager requestAlwaysAuthorization]; 
     } 
#endif 
      [self.locationManager startUpdatingLocation]; 
      [self.locationManager startUpdatingLocation]; 

      NSLog(@"%@", [self deviceLocation]); 

[super viewDidLoad]; 
}