Latitude & Longitude Not Retrieved

空扰寡人 提交于 2019-12-13 15:55:15

问题


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");
    }

}


回答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:




回答2:


1.first of all, you should use you should use a actual device instead of a simulator

2.implement CLLocationManagerDelegate in the interface,like

@interface XXXXX () <CLLocationManagerDelegate>

3.after

locationManager = [[CLLocationManager alloc] init];

add

locationManager.delegate = self;

4.implement the delegate method

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    for (CLLocation *location in locations) {
        //you can get locations here

    }
}


来源:https://stackoverflow.com/questions/34192331/latitude-longitude-not-retrieved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!