学习笔记 - 地图

1.iOS系统定位

/*
定位服务的实现
1. 集成定位框架,导入头文件
2. 初始化定位管家,并设置代理
3. 判断设备的定位服务是否开启
4. 判断应用的定位授权状态
5. 设置定位的基本属性
6. 开始定位
*/

//集成定位框架,导入头文件
#import 

//初始化定位管家,并设置代理(delegate)
@property(nonatomic,strong)CLLocationManager *manager;

//判断设备的定位服务是否开启
if ([CLLocationManager locationServicesEnabled]) {

       NSLog(@"定位服务已开启");

   }else{

       NSLog(@"定位服务未开启");
       return;

   }

// 判断定位服务授权的状态
if ([CLLocationManager authorizationStatus] == 0) {
       if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {

           // 授权请求需要对应不同的info设置
           [self.manager requestAlwaysAuthorization];
           //在info.plist中添加以下属性
           // NSLocationAlwaysUsageDescription
       }

   }

   // 设置定位精度
      self.manager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

      // 设置定位距离,避免定位过于频繁
      // self.locationManager.distanceFilter = 10;

      // 开始定位
      [self.manager startUpdatingLocation];

#parama mark -- 代理方法
        - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

          NSLog(@"++++++");
          [self.manager stopUpdatingLocation];// 停止定位服务

      }

2.地理编码与反编码

/** 地理编码器 */
@property (nonatomic, strong)CLGeocoder *geocoder;

//1. 初始化地理编码器(懒加载)
- (CLGeocoder *)geocoder {

    if (_geocoder == nil) {

        _geocoder = [[CLGeocoder alloc] init];
    }

    return _geocoder;
}
//地理编码
-(void)sssadas{
    // 1.地理编码  将地理名称转换成地理坐标
    NSString *addressName = @"邹城";
    [self.geocoder geocodeAddressString:addressName completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {

        if (error) {
            NSLog(@"%@",error);
            return ;
        }

        // 2.遍历地理编码获取的每一个地标
        //        CLPlacemark  封装了地理位置全部的地理信息
        for (CLPlacemark *pm in placemarks) {

            NSLog(@"+++++++");
            NSLog(@"%@",pm.location);// 地理坐标
            NSLog(@"%@",pm.addressDictionary);// 详细地理信息

        }
    }];

}
//反地理编码
-(void)locationss{
    // 反地理编码
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:37.785834 longitude:80.406417];

    [self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {

        if (error) {

            return ;
        }

        for (CLPlacemark *pm in placemarks) {

            NSLog(@"++++++");
            NSLog(@"%@",pm.addressDictionary);
        }
    }];
}

3.MapView

1. 首先要判断设备定位服务是否开启和应用的定位授权状态
2. 添加地图视图
@property (weak, nonatomic) MKMapView *mapView;
- (MKMapView *)mapView {

    if (_mapView == nil) {

        _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
        _mapView.delegate = self;

        _mapView.mapType = MKMapTypeHybrid;// 混合模式地图
        _mapView.userTrackingMode = MKUserTrackingModeFollow;// 跟踪用户位置

    }

    return _mapView;
}

//对地图属性进行设计
self.mapView.delegate = self;
self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
self.mapView.mapType = MKMapTypeStandard;
self.mapView.showsUserLocation = YES;

//地图操作
self.mapView.zoomEnabled = YES;// 可以缩放
self.mapView.scrollEnabled = YES;// 可以滚动
self.mapView.rotateEnabled = YES;// 可以旋转
self.mapView.pitchEnabled = YES;// 是否显示3D


self.mapView.showsCompass = YES;// 指南针
self.mapView.showsScale = YES;// 显示比例尺

#pragma mark --- 代理方法
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {

    NSLog(@"地图视图结束加载");
}

// 定位到用户的位置时调用该方法
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    NSLog(@"%f     %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    userLocation.title = @"北京";
    userLocation.subtitle = @"中国帝都";


}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    NSLog(@"地图显示的范围发生改变");
}

4.添加大头针

// 1. 给mapView添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

[self.mapView addGestureRecognizer:tap];
//2. 点击手势的方法
- (void)tapAction:(UITapGestureRecognizer *)tap{

    CGPoint point = [tap locationInView:tap.view];

    NSLog(@"触摸点的数学坐标%@",NSStringFromCGPoint(point));

    // 将数学坐标转换成地理坐标
     CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    NSLog(@"纬度%f    经度%f",coordinate.latitude,coordinate.longitude);

    // 仿照MKUserLocation自定义大头针视图模型
    /*
    只要一个NSObject类实现MKAnnotation协议就可以作为一个大头针,通常会重写协议中coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性,
    然后在程序中创建大头针对象并调用addAnnotation:方法添加大头针即可(之所以iOS没有定义一个基类实现这个协议供开发者使用,多数原因应该
    是MKAnnotation是一个模型对象,对于多数应用模型会稍有不同,例如后面的内容中会给大头针模型对象添加其他属性)。
    */
    CustomAnn *ani = [[CustomAnn alloc] init];//自定义的大头针
    ani.title = @"asda";
    ani.subtitle = @"dasdasd";

    ani.coordinate = coordinate;

    [self.mapView addAnnotation:ani];

}

#pragma mark MKMapViewDelegate (代理方法)
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        // 初始化区域中心
        CLLocationCoordinate2D center = userLocation.coordinate;
        // 初始化区域范围
        MKCoordinateSpan span = MKCoordinateSpanMake(1, 1*414/736);
        // 初始化区域
        MKCoordinateRegion regin = MKCoordinateRegionMake(center, span);
        // 设置地图显示的区域
        [self.mapView setRegion:regin animated:YES];
    });

}

你可能感兴趣的:(学习笔记 - 地图)