我想根據我的座標獲取我當前的位置。我在我的viewController
中實施CLLocationManager
,名爲myLocation
。 我的問題是,我第一次沒有得到我的座標,但是當我再次接近時,我得到了座標。我無法理解這個問題,爲什麼這不是第一次出現。 我也試過給NSTimer
stoplocation
,但是仍然無法第一次得到結果,每次我得到一個(空)值,然後得到座標。獲取座標CLLocationManager IOS
我的代碼:
#import <UIKit/UIKit.h>
#import <Corelocation/CoreLocation.h>
@interface myLocation : UITableViewController<CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@interface myLocation() {
CLLocationManager* _locationManager;
NSString * _lat;
NSString * _lng;
}
@end
@implementation myLocation
- (void)viewDidLoad
{
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[_locationManager requestWhenInUseAuthorization];
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
[_locationManager stopUpdatingLocation];
_lat =[NSString stringWithFormat:@"%f",location.coordinate.latitude];
_lng =[NSString stringWithFormat:@"%f",location.coordinate.longitude];
}
- (void)viewWillAppear:(BOOL) animated
{
NSLOG(@"%@",_lat);
NSLOG(@"%@",_lng);
}
哦,你的意思是爲什麼你的座標打印在viewWillAppear? –
是的,只有第一次。 @LyndseyScott – developer