码迷,mamicode.com
首页 > 移动开发 > 详细

iOS (地图)CLLocation

时间:2015-09-14 22:33:34      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:

//.h文件
#pragma mark---地理编码

- (IBAction)geocode:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UITextField *addressTextField;

@property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;

@property (strong, nonatomic) IBOutlet UILabel *detailAddress;


#pragma mark ----反地理编码

- (IBAction)reverseGetcode:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UITextField *longitudeTextField;


@property (strong, nonatomic) IBOutlet UITextField *latitudeTextfiled;

@property (strong, nonatomic) IBOutlet UILabel *addressLabel;

 

.m文件

@interface ViewController ()<CLLocationManagerDelegate>

@property(nonatomic,strong)CLLocationManager * locManager;//定位管理对象

@property(nonatomic,strong)CLGeocoder * geocoder;//编码器对象


@end

 

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //判断ios系统的版本 如果大于8.0就要获取授权  
    if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {
        //获取授权
        [ self.locManager requestWhenInUseAuthorization]; 
    }
    
    //每隔多少米定位一次
//    self.locManager.distanceFilter=10;
    
    //定位精确度(越精确就越耗电)
//    self.locManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
    
    //开始定位
    [ self.locManager startUpdatingLocation];
}


#pragma mark----实现locationManager的代理方法
#pragma mark----只要定位到用户的位置,就会调用(调用频率特别高)
//locations :装着CALocation对象
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    //1.取出位置对象
    CLLocation * loc =[locations firstObject];
    
    //2.取出经纬度
    //经度
    double longitude=loc.coordinate.longitude;
    
    //纬度
    double latitude=loc.coordinate.latitude;
    
    //打印经纬度
    NSLog(@"经度:%2.f 纬度:%2.f ",longitude,latitude);
    
    //3.打印经纬度
    NSLog(@"开始定位!----%@",locations);

    //停止定位(省电措施:只要不想定位服务,就马上停止定位服务
    [self.locManager stopUpdatingLocation];
}

//计算2个经纬度之间的直线距离
- (void)countLineDistance{
    
    CLLocation * cation1 =[[CLLocation alloc]initWithLatitude:40 longitude:116];

    
     CLLocation * cation2 =[[CLLocation alloc]initWithLatitude:41 longitude:116];
    
    CLLocationDistance distance= [cation1 distanceFromLocation:cation2];
    
    NSLog(@"%f",distance);
    
}

 

 

同时还要在info文件中添加 NSLocationWhenInUseUsageDescription

技术分享

 

//懒加载
-(CLLocationManager *)locManager{
    
    if (![CLLocationManager locationServicesEnabled]) {
        return nil;
    }
    
    //判断定位管理是否存在
    if (!_locManager) {
        //创建定位管理者
        _locManager =[[CLLocationManager alloc]init];
        
        _locManager.delegate=self;
    }
    
    return _locManager;
    
}

-(CLGeocoder *)geocoder{
    
    if (!_geocoder) {
        
        _geocoder =[[CLGeocoder alloc]init];
    }
    return _geocoder;
}

 

地理编码(根据地址定位出经纬度)

- (IBAction)geocode:(UIButton *)sender {
    
    if ([self.addressTextField.text length] ==0) {
        return;
    }

    NSLog(@"%@",self.addressTextField.text );
    
    //地理编码
       [self.geocoder geocodeAddressString:self.addressTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {
          
           if (error) {//有错误(地址乱输入)
               
               self.detailAddress.text=@"您输入的地名有错误!!";
        
           }else{
               
             CLPlacemark * placemark =[placemarks firstObject];
               
               self.detailAddress.text=placemark.name;
               /*
                name :名称
                locality:城市
                county:国家
                postanCode:邮政编码
                
                */
               
               self.longitudeLabel.text=[NSString stringWithFormat:@"%.2f",placemark.location.coordinate.longitude ];
               self.latitudeLabel.text=[NSString stringWithFormat:@"%2f",placemark.location.coordinate.latitude];
               
           }
           
       }];
 
   }

效果

技术分享

地理反编码 (根据经纬度定位出地址)

//反地理编码
- (IBAction)reverseGetcode:(UIButton *)sender {
    
    NSString * longitude =self.longitudeTextField.text;
    NSString * latitude =self.latitudeTextfiled.text;
    
    if (longitude.length==0 || latitude.length==0) {
        
        return;
    }
    
    CLLocation * location =[[CLLocation alloc]initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
    
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
       
        if (error) {
            
             self.addressLabel.text=@"你输入的经纬度错误!!";
        }else{
           
            CLPlacemark * pm=[placemarks firstObject];
            
            self.addressLabel.text=pm.name;
        }
        
    }];
}

 效果如下图

技术分享

 

iOS (地图)CLLocation

标签:

原文地址:http://www.cnblogs.com/wangbinbin/p/4808336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!