2014-08-29 63 views
-2
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    config.URLCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024 
                diskCapacity:10 * 1024 * 1024 
                 diskPath:@"MarkerData"]; 
    self.markerSession = [NSURLSession sessionWithConfiguration:config]; 


    locationManager = [[CLLocationManager alloc] init]; 
    NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO"); 

    CLLocationManager *lm = [[CLLocationManager alloc] init]; 
    lm.delegate = self; 
    lm.desiredAccuracy = kCLLocationAccuracyBest; 
    lm.distanceFilter = kCLDistanceFilterNone; 
    [lm startUpdatingLocation]; 
    //[lm stopUpdatingLocation]; 

    CLLocation *location = [lm location]; 

    CLLocationCoordinate2D coord; 
    coord.longitude = location.coordinate.longitude; 
    NSString *ss= [NSString stringWithFormat:@"%.8f",coord.longitude ]; 

    coord.latitude = location.coordinate.latitude; 
    NSString *aa= [NSString stringWithFormat:@"%.8f",coord.latitude ]; 
    NSLog(@"oooooooo2"); 
    NSLog(ss); 
    NSLog(aa); 
    NSLog(@"kkkkkkkkk2"); 

} 

回答

2

CLLocationManager是異步的。當底部的代碼執行時,它不會有機會獲得位置。你應該從CLLocationManagerDelegate–locationManager:didUpdateLocations:實現方法:

@interface MyClass() <CLLocationManagerDelegate> 

@end 

@implementation MyClass 

- (void)viewDidLoad 
{ 
    ... 
    CLLocationManager *lm = [[CLLocationManager alloc] init]; 
    lm.delegate = self; 
    ... 
} 

- (void)locationManager:(CLLocationManager *)locationManager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *location = [locationManager location]; 
    CLLocationCoordinate2D coord; 
    coord.longitude = location.coordinate.longitude; 
    etc... 
} 

@end 

有人也取得了block-based version,如果你進入的是諸如此類的事情。

相關問題