IOS位置和地图 CoreLocation框架

介绍

CoreLocation框架对iOS设备的定位功能进行了封装,通过CoreLocation框架可以获取iOS设备的地理位置信息,包括设备的经纬度、海拔等。在CoreLocation框架中,有CLLocationManager以及CLLocation两个核心类需要重点掌握。

CLLocationManager类常用功能介绍

CLLocationManager类用于定位服务管理类,当我们需要获取设备的位置信息时,都需要通过CLLocationManager类来统一管理。通过CLLocationManager类,我们不仅可以获取位置信息,也可以监控设备进入或离开某个区域,还可以获得设备的运行方向。

  • 获取设备最新的位置信息。
@property(readonly, nonatomic, copy, nullable) CLLocation *location;
  • 设置定位的精度。
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;

desiredAccuracy是一个很重要的属性,它有如下六个取值,需要根据应用的类别来具体设置,既要考虑到精度,同时还需要考虑设备的耗电情况。

extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation ;//导航情况下的最高精度,一般有外接电源时使用。
extern const CLLocationAccuracy kCLLocationAccuracyBest;//设备使用电池供电时的最高精度
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;//精确到10米
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;//精确到100米
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;//精确到1000米
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;//精确到3000米
  • 设置设备移动后获得位置信息的最小距离。
@property(assign, nonatomic) CLLocationDistance distanceFilter;

CLLocationManager类的代理对象

@property(assign, nonatomic, nullable)id delegate;

向用户请求允许在应用使用期间获取位置信息。

- (void)requestWhenInUseAuthorization;

向用户请求允许在任何时间获取位置信息。

- (void)requestAlwaysAuthorization;

开始定位。

- (void)startUpdatingLocation;

停止定位。

- (void)stopUpdatingLocation;

CLLocationManagerDelegate代理协议

CLLocationManagerDelegate代理协议中提供了用于监测设备定位相关的代理方法,常用的有如下几个。

定位成功时调用。在locations参数中,我们可以获取到设备当前的位置信息。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;

定位失败时调用。

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;

授权状态发生变化调用。

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

CLLocation类常用功能介绍

CLLocation类封装了iOS设备的经纬度、海拔等信息。当我们需要获取设备的位置信息时,通常情况下都需要从CLLocationManager类的location属性中获取。在CLLocation类中,定义了如下几个与地理位置相关的信息。
获取设备的经纬度。

@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;

获取设备的海拔。

@property(readonly, nonatomic) CLLocationDistance altitude;

获取设备的移动速度(单位是m/s)。

@property(readonly, nonatomic) CLLocationSpeed speed;

当前位置信息获取的时间

@property(readonly, nonatomic, copy) NSDate *timestamp;

基本使用举例

准备1:在使用CoreLocation框架之前,需要提前导入CoreLocation框架。
在这里插入图片描述
准备2:在info.plist文件中,添加提示信息,用户请求用户授权使用设备的位置信息。
在这里插入图片描述
在这里插入图片描述

//
//  ViewController.m
//  Location
//
//  Created by  on 2019/8/4.
//  Copyright © 2019 Shae. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *lngTextFied;
@property (weak, nonatomic) IBOutlet UITextField *latTextField;
@property (weak, nonatomic) IBOutlet UITextField *heightTextField;
@property (nonatomic,strong) CLLocationManager *locationManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.locationManager startUpdatingLocation];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.locationManager stopUpdatingLocation];
}
- (CLLocationManager *)locationManager{
    if (_locationManager==nil) {
        _locationManager=[[CLLocationManager alloc]init];
        _locationManager.desiredAccuracy=kCLLocationAccuracyBest;//精度设置
        _locationManager.distanceFilter=1000.0f;//设备移动后获取位置信息的最小距离
        _locationManager.delegate=self;
        [_locationManager requestWhenInUseAuthorization];//弹出用户授权对话框,使用程序期间授权
    }
    return _locationManager;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    self.lngTextFied.text=[NSString stringWithFormat:@"%3.5f",self.locationManager.location.coordinate.longitude];//获取经度
    self.latTextField.text=[NSString stringWithFormat:@"%3.5f",self.locationManager.location.coordinate.latitude];//获取纬度
    self.heightTextField.text=[NSString stringWithFormat:@"%3.5f",self.locationManager.location.altitude];//获取高度
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"E:%@",error);
}
@end

地理信息编码简介

CLGeocoder类简介

CLGeocoder类是在CoreLocation框架中提供的用于翻译地理位置的类,通过CLGeocoder类可以获取具体地理位置的经纬度信息,同时还可以根据经纬度来获取具体的位置信息。在CLGeocoder类中,提供了如下一些常用的方法和属性。
根据地理位置名称获取经纬度

- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

根据经纬度获取地理位置名称

- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

举例1由地址获取经纬度

模拟器默认的配置种”小地球“没有中文,只能输入英文。加入中文方法如下:

找到模拟器的Settings—>General–>Keyboard–>International KeyBoards–>Add New Keyboard–>Chinese Simplified(PinYin) 即我们一般用的简体中文拼音输入法,配置好后,再输入文字时,点击小地球,就可以选择输入中文了。

//点击获取位置的经纬度d
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UITextField *lngTextField1;
@property (weak, nonatomic) IBOutlet UITextField *latTextField1;
#pragma -mark 点击获取位置的经纬度 -
- (CLGeocoder *)geocoder{
    if (_geocoder==nil) {
        _geocoder=[[CLGeocoder alloc]init];
    }
    return _geocoder;
}
- (IBAction)clicked:(UIButton *)sender {
    //判断用户输入地址的有效性
    NSString *address  = self.addressField.text;
    if ([address length] == 0) {
        return;
    }
    [self.geocoder geocodeAddressString:self.addressField.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //判断是否有返回信息
        if ([placemarks count] > 0) {
            // placemarks数组中第一个为地标信息
            CLPlacemark *placemark = placemarks[0];
            //获得相应的经纬度
            CLLocationCoordinate2D coordinate = placemark.location.coordinate;
            //显示到对应的label上面
            self.latTextField1.text = [NSString stringWithFormat:@"%3.5f",coordinate.latitude];
            self.lngTextField1.text = [NSString stringWithFormat:@"%3.5f",coordinate.longitude];
        }
    }];
}

在这里插入图片描述

举例2由经纬度获取地址

//获取经度纬度的地址
@property (weak, nonatomic) IBOutlet UITextField *lngTextField2;
@property (weak, nonatomic) IBOutlet UITextField *latTextField2;
@property (weak, nonatomic) IBOutlet UITextField *addressField2;
@property (weak, nonatomic) IBOutlet UITextField *nameField2;
@property(nonatomic,strong)CLGeocoder *geocoder2;
#pragma -mark 获取经纬度的地址 -
- (CLGeocoder *)geocoder2{
    if (_geocoder2==nil) {
        _geocoder2=[[CLGeocoder alloc]init];
    }
    return _geocoder2;
}

- (IBAction)clicked2:(UIButton *)sender {
    double latitude=[self.latTextField2.text doubleValue];
    double longitude=[self.lngTextField2.text doubleValue];
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if ([placemarks count]>0) {
            //返回描述信息
            CLPlacemark *placemark=placemarks[0];
            //获取所需信息
            NSString *country=placemark.country;
            NSString *area=placemark.administrativeArea;
            NSString *city=placemark.locality;
            NSString *street=placemark.thoroughfare;
            //在textView上显示
            self.addressField2.text=[NSString stringWithFormat:@"%@ %@ %@ %@",country,area,city,street];
            self.nameField2.text=placemark.name;
        }
    }];
}

在这里插入图片描述

CLPlacemark类简介

当我们调用CLGeocoder类的geocodeAddressString:completionHandler:方法后,在该方法的回调block中可以返回CLPlacemark类的对象。CLPlacemark的字面意思是地标,地址的文字描述信息都封装在CLPlacemark类中,这里介绍几个CLPlacemark类中与文字描述相关的属性。

位置信息。CLLocation类的属性,可以获取位置对应的经纬度以及海拔高度等信息。

@property (nonatomic, readonly, copy, nullable) CLLocation *location;

地址信息的字典,包含一些地址信息的键值对。

@property (nonatomic, readonly, copy, nullable) NSDictionary *addressDictionary;

街道信息

@property (nonatomic, readonly, copy, nullable) NSString *thoroughfare; 

城市信息

@property (nonatomic, readonly, copy, nullable) NSString *locality; 

行政区信息

@property (nonatomic, readonly, copy, nullable) NSString *administrativeArea; 

国家信息

@property (nonatomic, readonly, copy, nullable) NSString *country; 

代码

https://github.com/ShaeZhuJiu/CoreLocation_base.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值