码迷,mamicode.com
首页 > 其他好文 > 详细

CoreLocation框架的使用

时间:2015-07-16 22:27:32      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

//地理编码类
//1、用来根据地名来解析当前的经纬度和其他一些附属消息
//2、根据经纬度来解析地名和其他消息

@interface ViewController ()<MKMapViewDelegate, CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

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

@property(nonatomic,strong)CLLocationManager* locationManager;


@end

@implementation ViewController

-(CLGeocoder *)coder{
    if(_coder==nil){
        _coder=[[CLGeocoder alloc] init];
    }
    return _coder;
}
-(CLLocationManager *)locationManager{
    if(_locationManager==nil){
        _locationManager=[[CLLocationManager alloc] init];
    }
    return _locationManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //ios8,获取用户位置,需要注册,否则报错Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
    //请求认证
    if([[UIDevice currentDevice].systemVersion floatValue]>8.0){
         [self.locationManager requestWhenInUseAuthorization ];//使用requestWhenInUseAuthorization也可以
    }
   
    
    //设置地图类型
    self.mapView.mapType = MKMapTypeStandard;
    
    
    self.mapView.delegate = self;
    
    //获取用户位置,需要注册
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    

    
}

#pragma mark - 地理编码和反地理编码
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    /*  1>地理编码**/
    //    烟台----37.4749,121.456
    
    
    NSString* address=@"烟台大学";
    [self.coder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"%ld",placemarks.count);////设置一下模拟器的语言就可以打印中文的。
        for(CLPlacemark* p in placemarks){
            NSLog(@"%@,%@,%@",p.locality,p.thoroughfare,p.name);
            NSLog(@"%g,%g",p.location.coordinate.latitude,p.location.coordinate.longitude);
        }
    }];
    
    
    
    
    
    /*  2>反地理编码**/
    
    CLLocation* location=[[CLLocation alloc] initWithLatitude:37.4749 longitude:121.456];//需要的CLLocationDegrees类型实质就是double
    [self.coder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        for(CLPlacemark* p in placemarks){
            NSLog(@"%@,%@,%@",p.name,p.locality,p.thoroughfare);
        }
    }];
    
 }

//MKMapView代理方法更改地图提示的文字
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    userLocation.title = @"北京";
    userLocation.subtitle = @"jjjjjjj";
}

//CLLocationManager代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    for (CLLocation * lo in locations)
    {
        NSLog(@"%g, %g", lo.coordinate.latitude,lo.coordinate.longitude);
        
    }
    
    [self.locationManager stopUpdatingLocation];
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

CoreLocation框架的使用

标签:

原文地址:http://blog.csdn.net/guodongsun0/article/details/46915935

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