地图(无界面的 简单功能)

首先引入库的头文件

//引入库的头文件
#import <CoreLocation/CoreLocation.h>

需要遵循的代理 以及声明所要用到的属性

@interface ViewController ()<CLLocationManagerDelegate>
//定位管理器
@property (nonatomic, strong) CLLocationManager *manager;
//编码反编码的类
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

具体操作:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //定位的步骤
    //第一步:初始化管理器
    self.manager = [[CLLocationManager alloc] init];
    //第二步:进行隐私的判断并授权

    //进行隐私的判断
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"是否前往隐私进行设置隐私定位");
    }
    //根据状态进行授权
    //进行版本的判断
    if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 8.0) {
        if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
            /*
             定位服务授权状态,返回枚举类型:
             kCLAuthorizationStatusNotDetermined: 用户尚未做出决定是否启用定位服务
             kCLAuthorizationStatusRestricted: 没有获得用户授权使用定位服务,可能用户没有自己禁止访问授权
             kCLAuthorizationStatusDenied :用户已经明确禁止应用使用定位服务或者当前系统定位服务处于关闭状态
             kCLAuthorizationStatusAuthorizedAlways: 应用获得授权可以一直使用定位服务,即使应用不在使用状态
             kCLAuthorizationStatusAuthorizedWhenInUse: 使用此应用过程中允许访问定位服务
             */

      /* ************* */    //在授权请求之前 需要在infoPlist中设置允许定位的内容:NSLocationWhenInUseUsageDescription 或者 NSLocationAlwaysUsageDescription

            //请求授权
            [self.manager requestWhenInUseAuthorization];
        }
    }
    //第三步设置管理的代理和相关属性
    self.manager.delegate = self;
    //设置精度
    self.manager.desiredAccuracy = 100;
    //设置最小更新距离
    self.manager.distanceFilter = 100;

    //第四步:开启定位
    [self.manager startUpdatingLocation];

    /*********************************编码和反编码*****************************************/

    //初始化对象
    self.geocoder = [[CLGeocoder alloc] init];

    //根据地名获取经纬度
  // [self getCoordinateByAdress:@"浏阳"];

    //根据经纬度反编码取出地名
  //  [self getAdressByLongitude:115 latitude:29.5];

    //计算两点间的距离
    [self distance];

}

- (void)getCoordinateByAdress:(NSString *)address{
    //编码方法
    [self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //根据返回的地标 取出第一个位置
        CLPlacemark *mark = placemarks.firstObject;

        //根据mark得到location
        CLLocation *location = mark.location;

        //根据mark获取区域
        CLRegion *region = mark.region;
        //获取addressDic
        NSDictionary *adressDict = mark.addressDictionary;

        NSLog(@"地表位置:%@,区域:%@,地理位置:%@", location, region, adressDict);
    }];
}


- (void)getAdressByLongitude:(CLLocationDegrees)longitude latitude:(CLLocationDegrees)latitude{
    //反编码
    //创建CLLocation
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        NSDictionary *dic = placemarks.firstObject.addressDictionary;

        NSLog(@"地名%@", dic);

    }];
}
#pragma mark - 计算两个位置的距离
- (void)distance{
   //计算位置1
    CLLocation *locationJiujiang = [[CLLocation alloc] initWithLatitude:29.5 longitude:116];
    //计算位置2
    CLLocation *locationliuyang = [[CLLocation alloc] initWithLatitude:28.1 longitude:113.6];
    CLLocationDistance distance = [locationliuyang distanceFromLocation:locationJiujiang];
    NSLog(@"%f",distance);

}


#pragma mark - CLLocationManagerDelegate代理方法
//这个代理方法是定位成功之后开始更新位置信息 只要移动了设置的最小距离之后就开始
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //获取最后一次的位置
    CLLocation *location = locations.lastObject;
    CLLocationCoordinate2D coorDinate = location.coordinate;
    NSLog(@"经度:%f,纬度:%f,海拔:%f,行走速度:%f", coorDinate.longitude, coorDinate.latitude, location.altitude, location.speed);

    //为节省电源 如果不使用定位 需要把定位关掉
    [self.manager stopUpdatingLocation];


}

//定位失败走的方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"定位失败了");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值