UPDATED for iOS9: I am new to iOS, experimenting with CoreLocation to return latitude and longitude values. The following code never executes the log output
-(void)viewWillAppear:(BOOL)animated{
manager = [[CLLocationManager alloc]init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation];
}
Here is my didUpdateToLocation function with is never reached
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"Location: %@", locations);
CLLocation *currentLocation = locations.lastObject;
if (currentLocation != nil) {
float latitude = currentLocation.coordinate.latitude;
float longitude = currentLocation.coordinate.longitude;
NSLog(@"dLongitude : %f", longitude);
NSLog(@"dLatitude : %f", latitude);
}
else{ NSLog(@"ERROR");
}
}
答案 0 :(得分:1)
CLLocationManager
is asynchronous, You should get the latitude and longitude in the delegate method.
locationManager.delegate = self;
Then implement this delegate method:
- locationManager:didUpdateLocations:
答案 1 :(得分:1)
1.首先,你应该使用你应该使用实际设备而不是模拟器
2.在界面中实现CLLocationManagerDelegate,如
@interface XXXXX () <CLLocationManagerDelegate>
3.之后
locationManager = [[CLLocationManager alloc] init];
添加
locationManager.delegate = self;
4.实施委托方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
for (CLLocation *location in locations) {
//you can get locations here
}
}