iOS定位,两地之间距离,编码与反编码

//
//  ViewController.m
//  location_Core Location
//
//  Created by bin chen on 16/1/26.
//  Copyright © 2016年 bin chen. All rights reserved.
//

#import "ViewController.h"
//导入接口文件
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>

@property (nonatomic,strong) CLLocationManager * locationManager;
@property (nonatomic,assign) float latitude;
@property (nonatomic,assign) float longitude;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //位置管理器实例
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    
    //使用前判断版本号
    if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 8.0) {
        //调用受权方法
        [self.locationManager requestWhenInUseAuthorization];
    }
    
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;  //指定位置精度
    self.locationManager.distanceFilter = 10.0f; //每移动200米发送一次更新给委托,即距离过滤器
    [self.locationManager startUpdatingLocation]; //启动位置更新,除非设置中止,不然一旦启动将会一直发送更新,即实时更新
    
    //        [self.locationManager stopUpdatingLocation];  //定位成功后中止定位
}

#pragma mark - 与位置相关的 -CLLocationManagerDelegate 方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{
    //locations 位置变化的集合, 其中每一个CLLocation对象都包含:coordinate(坐标)、altitude(海拔高度)、horizontalAccuracy(位置的精度-半径)、verticalAccuracy(海拔高度的精度)、speed(速度)
    
    CLLocation *curLocation = [locations lastObject];   //最新位置信息
    
    if (curLocation.horizontalAccuracy > 0) {
        NSLog(@"当前位置:%f,%f +/- %f meters", curLocation.coordinate.longitude, curLocation.coordinate.latitude, curLocation.horizontalAccuracy);
        
        //设一个目的地北京,计算模拟器中定位的香港到北京的距离
        CLLocation *destLocation = [[CLLocation alloc] initWithLatitude:39.904989 longitude:116.405285];
        CLLocationDistance distance = [destLocation distanceFromLocation:curLocation];
        
        NSLog(@"两地之间的距离: %f", distance);
        
        if (distance < 500) {
            [self.locationManager stopUpdatingLocation];  //中止定位
            NSLog(@"您已经到达目的地");
        }
        
        self.latitude = curLocation.coordinate.latitude;
        self.longitude = curLocation.coordinate.longitude;
        
        //反编码
        [self createCity];
        
    }
    
    if (curLocation.verticalAccuracy > 0) {
        NSLog(@"当前海拔高度%f +/- %f meters", curLocation.altitude, curLocation.verticalAccuracy);
    }
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error{
    
    if (error.code == kCLErrorLocationUnknown) {
        //没法确认位置
        NSLog(@"Currently unable to retrieve location");
    }else if (error.code == kCLErrorNetwork){
        //没法定位,请检查网络是否链接
        NSLog(@"Network used to retrieve location is unavailable.");
    }else if (error.code == kCLErrorDenied){
        //用户禁止应用程序定位
        NSLog(@"Permission to retrieve location is denied.");
        [manager stopUpdatingLocation]; //中止定位
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 反编码
- (void)createCity{
    //地理编码 ,反编码
    //经纬度--->位置信息
    CLGeocoder *gecoder = [[CLGeocoder alloc] init];
    CLLocation *location = [[CLLocation alloc]initWithLatitude: self.latitude longitude:self.longitude];
    
    
//    CLLocation *location = [[CLLocation alloc]initWithLatitude: 39.904989 longitude:116.405285];

    [gecoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            NSLog(@"反编码失败:%@",error);
        }else{
            NSLog(@"反编码成功");
            //NSLog(@"%@",placemarks);
            CLPlacemark *placemark = [placemarks lastObject];
            NSLog(@"%@ %@",placemark.name,placemark.thoroughfare);
            NSLog(@"%@",placemark.name);
            NSLog(@"%@",placemark.locality);
        }
    }];
    
}

#pragma mark - 编码
- (void)ReginWithCity:(NSString *)cityName{
    CLGeocoder *gecoder = [[CLGeocoder alloc] init];
    [gecoder geocodeAddressString:cityName completionHandler:^(NSArray *placemarks, NSError *error) {
        
        if (error) {
            NSLog(@"编码失败:%@",error);
        }else{
            NSLog(@"编码成功");
            
            CLPlacemark *place = [placemarks lastObject];
            CLLocationCoordinate2D coordinate = place.location.coordinate;
            
            NSLog(@"%f,%f",coordinate.latitude,coordinate.longitude);
        }
    }];
}

@end
相关文章
相关标签/搜索