如何在iOS上获取当前位置?

Mel*_*cuk 0 iphone objective-c core-location cllocationmanager ios

我无法获得当前位置.当我在不同的地方启动我的应用程序时,App可以获得最后的位置.但我不想最后的位置.如果您关闭应用并重新启动它,现在应用可以获取当前位置.即使首次启动应用程序,我如何获得当前位置?

- (void)viewDidLoad
{
    [super viewDidLoad];

    [locationManager startUpdatingLocation];

    self.mapView.delegate = self;
    [self.mapView setShowsUserLocation:YES];

    locationManager.delegate=self;

    locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=kCLDistanceFilterNone;

    location = [locationManager location];

    CLLocationCoordinate2D coord;
    coord.longitude = location.coordinate.longitude;
    coord.latitude = location.coordinate.latitude;
    lat = coord.latitude;
    longt = coord.longitude;
}
Run Code Online (Sandbox Code Playgroud)

Raj*_*071 8

[locationManager startUpdatingLocation];在设置其委托之前正在做

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

[locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)

并实现其委托方法

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{

}
Run Code Online (Sandbox Code Playgroud)