IOS开发控件视图day06:Location定位和Map地图的使用

1、导包和配置文件
(1)Info.plist文件设置访问权限
在这里插入图片描述(2)点击项目,在General中添加包
IOS开发控件视图day06:Location定位和Map地图的使用_第1张图片

2、Location获取当前位置坐标
(1)声明和关联控件

@interface LocationViewController ()<CLLocationManagerDelegate>
//位置管理者
@property (nonatomic,strong)CLLocationManager *manager;
@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
@property (weak, nonatomic) IBOutlet UILabel *lab3;
@end

(2)初始化

@implementation LocationViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    [self location];
}

- (CLLocationManager *)manager{
    if (!_manager) {
        _manager = [[CLLocationManager alloc] init];
    }
    return _manager;
}

(3)定位

-(void)location{
    //1、创建位置管理器
    //向用户请求我们的程序使用位置
    [self.manager requestAlwaysAuthorization];
    [self.manager requestWhenInUseAuthorization];
    //设置定位的更新距离(10米)
    self.manager.distanceFilter = 10.0f;
    //设置代理
    self.manager.delegate = self;
    //开启定位功能
    [self.manager startUpdatingLocation]; 
}

(4)获取位置与编码

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    //获得当前最新位置
    CLLocation *location = (CLLocation *)[locations lastObject];
    double lat = location.coordinate.latitude;
    double lon = location.coordinate.longitude;
    NSString *str1 = [NSString stringWithFormat:@" %f",lat];
    NSString *str2 = [NSString stringWithFormat:@" %f",lon];
    _lab1.text = str1;
    _lab2.text = str2;
    
    //反地理编码实现所在城市显示
    //1、创建一个CLGecoder对象
    CLGeocoder *geocoder = [CLGeocoder new];
    //2、实现反地理编码方法
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //3.1、预防处理
        if (placemarks.count == 0 || error) {
            NSLog(@"解析没有数据或者出错");
            return;
        }
        for (CLPlacemark *placemark in placemarks) {
            NSString *str3 = [NSString stringWithFormat:@"%@,%@",placemark.locality,placemark.name];
            self.lab3.text  = str3;
        } 
    }];
}

(5)停止定位

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.manager stopUpdatingLocation];
}

(6)实际效果
IOS开发控件视图day06:Location定位和Map地图的使用_第2张图片
IOS开发控件视图day06:Location定位和Map地图的使用_第3张图片
3、Map地图的定位、放大缩小以及地图样式
(1)声明和绑定控件

@interface MapViewController ()<MKMapViewDelegate>
@property (retain, nonatomic) IBOutlet MKMapView *mapView;
@property(nonatomic,strong)CLLocationManager *mgr;
@end

(2)位置管理器

 //1、创建位置管理器
 self.mgr  = [CLLocationManager new];
 //2、请求授权 -->plist
 if ([self.mgr respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
     [self.mgr requestWhenInUseAuthorization];
 }
 //3、设置显示用户位置
 self.mapView.userTrackingMode = MKUserTrackingModeFollow;
 //4、设置代理-->获取用户位置
 self.mapView.delegate = self;

(3)地图样式切换

- (IBAction)mapTypeChangeClick:(UISegmentedControl *)sender {
//    MKMapTypeStandard = 0, 默认
//    MKMapTypeSatellite,   卫星
//    MKMapTypeHybrid,      混合
//    MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
//    MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0),
//    MKMapTypeMutedStandard NS_ENUM_AVAILABLE(10_13, 11_0) __TVOS_AVAILABLE(11_0),
  switch (sender.selectedSegmentIndex) {
      case 0:
          self.mapView.mapType = MKMapTypeStandard;
          break;
      case 1:
          self.mapView.mapType = MKMapTypeSatellite;
          break;
      case 2:
          self.mapView.mapType = MKMapTypeHybrid;
          break;
      default:
          break;
  }  
}

(4)完成用户位置更新的时候,调用MKUserLocation:大头针模型

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //显示大头针信息 -->反地理编码实现
    //1、创建一个CLGecoder对象
    CLGeocoder *geocoder = [CLGeocoder new];
    //2、实现反地理编码方法
    [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        //3.1、预防处理
        if (placemarks.count == 0 || error) {
            return;
        }
        
        //3.2、获取对象
        CLPlacemark *placemark = placemarks.lastObject;
        
        //3.3、设置标题为城市信息
        userLocation.title  = placemark.locality;
        
        //3.4设置子标题为详细地址
        userLocation.subtitle = placemark.name;
        
    }];
    
}

(6)返回用户所在位置

- (IBAction)backUserLocationClick:(id)sender
{
    //1、设置中心点坐标
//    self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
    //2、设置范围属性
    //2.1、获取坐标
    CLLocationCoordinate2D coordinate = self.mapView.userLocation.location.coordinate;
    //2.2设置显示范围
    MKCoordinateSpan span = MKCoordinateSpanMake(0.028219, 0.017767);
    //设置范围方法,可以设置动画
    [self.mapView setRegion:MKCoordinateRegionMake(coordinate, span)animated:YES]; 
}
//当地图显示区域发生改变后,会调用的方法
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
//获取默认的显示大小 --> span
 NSLog(@"latitudeDelta:%f,longitudeDelta:%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta);    															
}

(7)放大地图

- (IBAction)zoomIn:(id)sender
{
    //1、Delta缩小一倍
    CGFloat latitude = self.mapView.region.span.latitudeDelta * 0.5;
    CGFloat longitude = self.mapView.region.span.longitudeDelta * 0.5;
    //2、重设region值
    [self.mapView setRegion:MKCoordinateRegionMake(self.mapView.region.center, MKCoordinateSpanMake(latitude, longitude))animated:YES];   
}

(8)缩小地图

- (IBAction)zoomOut:(id)sender
{
    //1、Delta扩大一倍
    CGFloat latitude = self.mapView.region.span.latitudeDelta * 2;
    CGFloat longitude = self.mapView.region.span.longitudeDelta * 2;
    //2、重设region值
    [self.mapView setRegion:MKCoordinateRegionMake(self.mapView.region.center, MKCoordinateSpanMake(latitude, longitude))animated:YES];
}

(9)实际效果
IOS开发控件视图day06:Location定位和Map地图的使用_第4张图片
IOS开发控件视图day06:Location定位和Map地图的使用_第5张图片

你可能感兴趣的:(IOS控件基础)