#pragma mark---iOS定位操作/**在iOS中通过CoreLocation定位,可以获取到用户当前位置,同时能得到装置移动信息 -——————————————————————————————————
1.需要添加CoreLocation.framework库 ——————————————————————————————
2.添加头文件#import<CoreLocation/CoreLocation.h>
3.CLLocationManager的对象要设置成全局的 ——————————————————————
4.注意iOS8前后的区别 ------------
5.监听用户授权状态
*/- (void)initCoreLocation{
self.title = @"iOS定位操作";
UIButton* showCameraBut = [UIButton buttonWithType:UIButtonTypeCustom]; [self.view addSubview:showCameraBut];
showCameraBut.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2.0-100/2.0, 80, 100, 30);
[showCameraBut setTitle:@"开始定位" forState:UIControlStateNormal]; [showCameraBut addTarget:self action:@selector(didCoreLocation) forControlEvents:UIControlEventTouchUpInside];
[showCameraBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; }
#pragma mark----显示位置信息- (void)didCoreLocation{ _locationManager = [[CLLocationManager alloc]init]; _locationManager.delegate = self;
[_locationManager startUpdatingLocation];//startUpdatingLocation//
/[locationManager requestWhenInUseAuthorization];
[_locationManager requestAlwaysAuthorization];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.distanceFilter = 10.0f;}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations{
CLLocation* newlocation = [locations objectAtIndex:0];
NSLog(@"latitude---%f",newlocation.coordinate.latitude);
NSLog(@"longitude---%f",newlocation.coordinate.longitude);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"oldLocation---latitude---%f",newLocation.coordinate.latitude);
NSLog(@"longitude---%f",newLocation.coordinate.longitude);
}
#pragma mark---监听用户授权状态
//当用户授权状态发生变化时调用
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
// 用户还未决定
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"用户还未决定");
break;
}
// 访问受限(苹果预留选项,暂时没用)
case kCLAuthorizationStatusRestricted:
{
NSLog(@"访问受限");
break;
}
// 定位关闭时和对此APP授权为never时调用
case kCLAuthorizationStatusDenied:
{
// 定位是否可用(是否支持定位或者定位是否开启)
if([CLLocationManager locationServicesEnabled])
{
NSLog(@"定位开启,但被拒");
// 在此处, 应该提醒用户给此应用授权, 并跳转到"设置"界面让用户进行授权
// 在iOS8.0之后跳转到"设置"界面代码
NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:settingURL])
{
// [[UIApplication sharedApplication] openURL:settingURL];
[[UIApplication sharedApplication]openURL:settingURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @YES} completionHandler:nil];
}
}else
{
NSLog(@"定位关闭,不可用");
}
break;
}
// 获取前后台定位授权
case kCLAuthorizationStatusAuthorizedAlways:
// case kCLAuthorizationStatusAuthorized: // 失效,不建议使用
{
NSLog(@"获取前后台定位授权");
break;
}
// 获得前台定位授权
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"获得前台定位授权");
break;
}
default:
break;
}}