iOS学习_地图_定位和编码与反编码

定位:

引入头文件  #import <CoreLocation/CoreLocation.h>
声明管理器属性:@property(nonatomic,strong)CLLocationManager *manager;
第一步:初始化管理器
self.manager = [[CLLocationManager alloc] init];
第二步:进行隐私的判断并授权

//进行隐私的判断
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"是否前往隐私进行设置,允许定位");
    }
    //进行版本的判断
    if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 8.0) {
        //判断授权状态
        if ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse) {
            //请求授权
            [self.manager requestWhenInUseAuthorization];
        }
    }

 

//在授权请求之前需要在inforplist中设置允许定位的内容:把NSLocationWhenInUseUsageDescription(NSLocationAlwaysUsageDescription这是另一种方式)输入到inforplist中,然后在后边输入一句话;

第三步:设置管理器的代理和属性

 

self.manager.delegate = self;

 

第四步:开启定位

 

 

    [self.manager startUpdatingLocation];

 

实现代理方法

 

 

//这个代理方法是定位成功之后开始更新位置信息,只要移动的设置的最小距离之后也开始走这个方法;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //获取最后一次的位置
    CLLocation *location = locations.lastObject;
    //获取位置的坐标
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"经度:%f,纬度:%f,海拔:%f,航海方向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
    //为了节省电源,如果不使用定位,需要把定位关了
    [self.manager stopUpdatingLocation];
}
//定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"定位失败");
}

 编码与反编码

声明编码与反编码类:@property(nonatomic,strong)CLGeocoder *geocoder;

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

根据地名获取相关的信息(编码)

- (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;
        //获取字典信息
        NSDictionary *addressDic = mark.addressDictionary;
        NSLog(@"地标位置:%@,区域%@,地理位置信息%@",location,region,addressDic);
        /*
        //        NSString *name=placemark.name;//地名
        //        NSString *thoroughfare=placemark.thoroughfare;//街道
        //        NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等
        //        NSString *locality=placemark.locality; // 城市
        //        NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑
        //        NSString *administrativeArea=placemark.administrativeArea; // 州
        //        NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息
        //        NSString *postalCode=placemark.postalCode; //邮编
        //        NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码
        //        NSString *country=placemark.country; //国家
        //        NSString *inlandWater=placemark.inlandWater; //水源、湖泊
        //        NSString *ocean=placemark.ocean; // 海洋
        //        NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标
         */
    }];
}

根据经纬度反编码取出地址(反编码)

- (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);
        
    }];
}

计算两点之间的距离

- (void)distance{
    //创建位置一
    CLLocation *locationBeijing = [[CLLocation alloc] initWithLatitude:40 longitude:116];
    CLLocation *locationDaLiang = [[CLLocation alloc] initWithLatitude:39 longitude:121];
    CLLocationDistance distance = [locationBeijing distanceFromLocation:locationDaLiang];
    NSLog(@"北京到大连的距离:%f",distance);
}

 

posted on 2016-05-31 17:43  哦嘿嘿  阅读(423)  评论(0编辑  收藏  举报