- (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");
}
答案 0 :(得分: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。