首页 技术 正文
技术 2022年11月10日
0 收藏 948 点赞 3,274 浏览 2874 个字

CoreLocation框架实现定位功能(iOS8.0之后)

//
// ViewController.m
// 定位
//
// Created by admin on 2017/9/20.
// Copyright © 2017年 admin. All rights reserved.
//#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)CLLocationManager *locationManager;@end@implementation ViewController- (void)viewDidLoad {
[super viewDidLoad];
[self startLocation];
}
-(void)startLocation
{
//判断用户是否打开了定位功能
if([CLLocationManager locationServicesEnabled]){
if(!_locationManager){
_locationManager=[[CLLocationManager alloc]init];
//设置代理
[self.locationManager setDelegate:self];
//设置定位精确度,精确度越高,越耗电
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//设置多远距离定位一次
[self.locationManager setDistanceFilter:100];
//开始获取授权,打开定位
[self.locationManager requestWhenInUseAuthorization];
//开始定位
[self.locationManager startUpdatingLocation];
}else{
[self.locationManager startUpdatingLocation];
}
}else{
NSLog(@"%d",666);
}
}#pragma mark -CLLocationManagerDelegate
//代理方法监听定位服务状态的变化
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[_locationManager requestAlwaysAuthorization];
NSLog(@"用户还未决定授权");
}
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[_locationManager requestAlwaysAuthorization];
NSLog(@"定位服务授权状态被允许在使用应用程序的时候");
}
break;
case kCLAuthorizationStatusRestricted:
{
NSLog(@"访问受限");
break;
}
case kCLAuthorizationStatusAuthorizedAlways:
if([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[_locationManager requestAlwaysAuthorization];
NSLog(@"定位服务授权状态已经被用户允许在任何状态下获取位置信息。包括监测区域、访问区域、或者在有显著的位置变化的时候");
}
break;
case kCLAuthorizationStatusDenied:
NSLog(@"被拒绝了");
break;
default:
break;
}
}
//代理方法返回locationd 信息
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"%@",locations);
CLLocation *currLocation=locations.lastObject;
NSTimeInterval locatinAge=-[currLocation.timestamp timeIntervalSinceNow];
NSLog(@"%f----%f",[currLocation.timestamp timeIntervalSince1970],locatinAge);
//关闭定位
[self.locationManager stopUpdatingLocation];
CLLocation *location=locations.lastObject;
[self reverseGeocoder:location];
}
//地理反编码
-(void)reverseGeocoder:(CLLocation *)currentLocation{
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(error || placemarks.count==0){
NSLog(@"反编码失败");
}else{
CLPlacemark *placemark=placemarks.firstObject;
NSLog(@"placemark:%@",[placemark addressDictionary]);
NSString *city=[[placemark addressDictionary]objectForKey:@"City"];
NSLog(@"%@",city);
}
}];
}@end
github:https://github.com/Frankltf/ios-CoreLocation/tree/features-one
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898