CoreLocation的使用 定位 既地理编码与反地理编码

来源:互联网 发布:天仕博软件 编辑:程序博客网 时间:2024/05/16 07:02

地理编码即用经纬度获取详细的地理信息

反地理编码即用模糊的地理信息获取相应的地理信息

两点之间的距离的计算


////  ViewController.m//  2014_11_03_CoreLocation的使用////  Created by Mac10.9 on 14-11-3.//  Copyright (c) 2014年 xiaoxiaobing. All rights reserved.#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate , UIActionSheetDelegate]] >@property (nonatomic, strong) CLLocationManager *locMgr;#pragma mark - 解码/** *  解码器 */@property (nonatomic, strong) CLGeocoder *geocoder;#pragma mark - 地理编码- (IBAction)geocode;@property (weak, nonatomic) IBOutlet UITextField *addressField;@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;#pragma mark - 反地理编码- (IBAction)reverseGeocode;@property (weak, nonatomic) IBOutlet UITextField *longtitudeField;@property (weak, nonatomic) IBOutlet UITextField *latitudeField;@property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel;@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    [self setupCLLocationManager];}/** *  对CLLocationManager 进行初始化 */- (void)setupCLLocationManager{        // 开始定位用户的位置        [self.locMgr startUpdatingLocation];    //询问永久定位服务    [self.locMgr requestAlwaysAuthorization];        //    // 开始监控某个位置 (在某个范围里该干什么事,除了范围又要干什么事)    //    CLRegion *region = [[CLRegion alloc] init];    //    /**    //     ........    //     */    //    [self.locMgr startMonitoringForRegion:region];}/** *  懒加载 */- (CLLocationManager *)locMgr{#warning 定位服务不可用    if(![CLLocationManager locationServicesEnabled])    {        /**         只有系统的定位功能是关闭的时候才会调用这个方法         */        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"定位不可用" delegate:self cancelButtonTitle:@"请去系统中打开定位功能" destructiveButtonTitle:@"取消" otherButtonTitles: nil];        [sheet showInView:self.view];        return nil;    }        if (!_locMgr)    {        // 创建定位管理者        self.locMgr = [[CLLocationManager alloc] init];        // 设置代理        self.locMgr.delegate = self;    }    return _locMgr;}/** *  计算两个点之间的距离 */- (void)countLineDistance{    // 计算2个经纬度之间的直线距离    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:40 longitude:116];    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:41 longitude:116];    CLLocationDistance distance = [loc1 distanceFromLocation:loc2];    NSLog(@"%f", distance);}#pragma mark - CLLocationManagerDelegate/** *  只要定位到用户的位置,就会调用(调用频率特别高) *  @param locations : 装着CLLocation对象 */- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    // 1.取出位置对象    CLLocation *loc = [locations firstObject];    // 2.取出经纬度    CLLocationCoordinate2D  coordinate2D = loc.coordinate;        // 3.打印经纬度        NSLog(@"维度%lf  经度%lf",coordinate2D.latitude , coordinate2D.longitude);    // 停止定位(省电措施:只要不想用定位服务,就马上停止定位服务)    [manager stopUpdatingLocation];}- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    }#pragma mark -解码相关- (CLGeocoder *)geocoder{    if (!_geocoder) {        self.geocoder = [[CLGeocoder alloc] init];    }    return _geocoder;}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    [self.view endEditing:YES];}/** *  地理编码 */- (IBAction)geocode {    NSLog(@"地理编码ing。。。。。。");    NSString *address = self.addressField.text;    if (address.length == 0) return;        [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {        if (error) { // 有错误(地址乱输入)            self.detailAddressLabel.text = @"你找的地址可能只在火星有!!!";        } else { // 编码成功            // 取出最前面的地址            CLPlacemark *pm = [placemarks firstObject];                        // 设置经纬度            self.latitudeLabel.text = [NSString stringWithFormat:@"%.1f", pm.location.coordinate.latitude];            self.longitudeLabel.text = [NSString stringWithFormat:@"%.1f", pm.location.coordinate.longitude];                        // 设置具体地址            self.detailAddressLabel.text = pm.name;                        //            NSLog(@"总共找到%d个地址", placemarks.count);            //            //            for (CLPlacemark *pm in placemarks) {            //                NSLog(@"-----地址开始----");            //            //                NSLog(@"%f %f %@", pm.location.coordinate.latitude, pm.location.coordinate.longitude, pm.name);            //            //                [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {            //                    NSLog(@"%@ %@", key, obj);            //                }];            //            //                NSLog(@"-----地址结束----");            //            }        }    }];}/** *  反地理编码 */- (IBAction)reverseGeocode {    // 1.包装位置    CLLocationDegrees latitude = [self.latitudeField.text doubleValue];    CLLocationDegrees longitude = [self.longtitudeField.text doubleValue];    CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];        // 2.反地理编码    [self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {        if (error) { // 有错误(地址乱输入)            self.reverseDetailAddressLabel.text = @"你找的地址可能只在火星有!!!";        } else { // 编码成功            // 取出最前面的地址            CLPlacemark *pm = [placemarks firstObject];                        // 设置具体地址            self.reverseDetailAddressLabel.text = pm.name;            //            NSLog(@"总共找到%d个地址", placemarks.count);            //            //            for (CLPlacemark *pm in placemarks) {            //                NSLog(@"-----地址开始----");            //            //                NSLog(@"%f %f %@", pm.location.coordinate.latitude, pm.location.coordinate.longitude, pm.name);            //            //                [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {            //                    NSLog(@"%@ %@", key, obj);            //                }];            //            //                NSLog(@"-----地址结束----");            //            }        }    }];}@end


0 0
原创粉丝点击