获取坐标CLLocationManager IOS
我正试图根据我的坐标精确地确定我当前的位置。我在名为获取坐标CLLocationManager IOS,ios,gps,cllocationmanager,cllocation,Ios,Gps,Cllocationmanager,Cllocation,我正试图根据我的坐标精确地确定我当前的位置。我在名为myLocation的viewController中实现了CLLocationManager。 我的问题是,我第一次没有得到坐标,但当我再次接近时,我得到了坐标。我无法理解为什么这个问题没有第一次出现。 我还尝试给stoplocation提供一个NSTimer,但第一次还是无法得到结果,每次我都得到一个(null)值,然后得到坐标 我的代码: #import <UIKit/UIKit.h> #import <Corelocat
myLocation
的viewController
中实现了CLLocationManager
。
我的问题是,我第一次没有得到坐标,但当我再次接近时,我得到了坐标。我无法理解为什么这个问题没有第一次出现。
我还尝试给stoplocation
提供一个NSTimer
,但第一次还是无法得到结果,每次我都得到一个(null)值,然后得到坐标
我的代码:
#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);
}
#导入
#进口
@接口位置:UITableViewController
@属性(强,非原子)CLLocationManager*locationManager;
@结束
@接口位置(){
CLLocationManager*_locationManager;
NSString*_lat;
液化天然气;
}
@结束
@实施myLocation
-(无效)viewDidLoad
{
[超级视图下载];
_locationManager=[[CLLocationManager alloc]init];
_locationManager.distanceFilter=KCLDistanceFilterOne;
_locationManager.delegate=self;
_locationManager.desiredAccuracy=KCallocationAccuracyHundredMeters;
如果([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
[_locationManager请求使用授权];
[_LocationManagerStartupDatingLocation];
}
-(无效)locationManager:(CLLocationManager*)经理
didUpdateLocations:(NSArray*)位置{
CLLocation*location=[locations lastObject];
[\u locationManager停止更新位置];
_lat=[NSString stringWithFormat:@“%f”,位置.坐标.纬度];
_lng=[NSString stringWithFormat:@“%f”,位置.坐标.经度];
}
-(无效)视图将显示:(BOOL)动画
{
NSLOG(@“%@”,_lat);
NSLOG(@“%@”,液化天然气);
}
当您尝试在视图中打印坐标时,坐标尚未显示。
因为CLLocationManager
还没有足够的时间检索第一个位置。在尝试使用设备坐标之前,请等待第一次调用didUpdateLocations:
,因为您将在那里接收这些坐标。我建议您删除从视图中打印坐标代码的尝试,将显示,只需在didUpdateLocations:
中打印即可
在评论中,OP表示他希望在视图出现期间“刷新”位置。我建议在视图消失时停止更新,并在视图重新出现时重新启动更新:
- (void)viewWillAppear:(BOOL) animated
{
[_locationManager startUpdatingLocation];
}
- (void)viewWillDisappear:(BOOL) animated
{
[_locationManager stopUpdatingLocation];
}
位置服务启动并调用您的委托方法需要一些时间-如果您仅在viewDidLoad
中启动位置服务,则在调用viewwillbeen
之前几乎肯定不会发生这种情况。此外,应用程序第一次执行时必须等待用户授予权限
您可以检查CLLocationManager
的location
属性以获取最近的位置。如果它是nil
,那么还没有确定位置。哦,你的意思是为什么不在视图中打印坐标?是的,只是第一次@Lyndseyscotty你的建议对我很有帮助,我的秘密中还有一点问题。我只是在视图中调用它,这样每当它新加载时,无论何时单击,它都会刷新。一种可能的情况是,若用户并没有互联网连接,在那个之后,他可以对refesh做什么。有什么建议吗?我得到了解决方案,viewwillapear中的初始化帮助了我。Thanks@user3166680实际上,我只是输入了一个类似的建议,但不需要完全重新初始化您的位置管理器。我更新了我的答案,建议在视图消失并重新出现时停止并重新启动位置管理器。