11.3 地图 系统定位

/延展Extension类别 Category

@interface ZYViewController ()<CLLocationManagerDelegate>

{

    //位置管理器

    CLLocationManager *_locationManager;

    

    //地理编码器

    CLGeocoder *_geoCoder;

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    //判断是否创建过

    if (nil == _locationManager)

    {

        //创建位置管理器

        _locationManager = [[CLLocationManager alloc] init];

        //设置代理

        _locationManager.delegate = self;

    }

    

    if (nil == _geoCoder)

    {

        //创建并初始化编码器

        _geoCoder = [[CLGeocoder alloc] init];

    }

    

}


//1、开始定位

- (IBAction)startLocation:(id)sender

{

    //判断定位服务是否可用

    if ([CLLocationManager locationServicesEnabled])

    {

        //响应位置变化的距离默认值是kCLDistanceFilterNone 所有的移动事件都会通知客户端

        //也可以指定一个值单位M

        _locationManager.distanceFilter = 1000.0;

        

        //要求精度默认值是kCLLocationAccuracyBest

        //精度有十米百米 千米 三千米

        _locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;

        

        //开始定位后台定位服务开启

        [_locationManager startUpdatingLocation];

    }

    else

    {

        NSLog(@"定位服务不可用");

    }

}


//2、更新方向

- (IBAction)startHeading:(id)sender

{

    //方向服务是否可用

    if ([CLLocationManager headingAvailable])

    {

        //kCLDistanceFilterNone

        _locationManager.distanceFilter = kCLDistanceFilterNone;

        //开启更新服务

        [_locationManager startUpdatingHeading];

    }

    else

    {

        NSLog(@"方向服务不可用");

    }

}


//地理编码 根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)

- (IBAction)geocodeAddressString:(id)sender

{

    NSString *locationString = @"经北三路第五大街";

    //placemarks数组中存放位置信息

    [_geoCoder geocodeAddressString:locationString completionHandler:^(NSArray *placemarks, NSError *error) {

        //判断编码是否成功

        if (placemarks.count != 0 && error == nil)

        {

            NSLog(@"placemarks_____%@", placemarks);

            CLPlacemark *place = [placemarks lastObject];

            NSString *name = place.name;

            NSString *thoroughfare = place.thoroughfare;

            NSString *locality = place.locality;

            NSString *ISOcountryCode =  place.ISOcountryCode;

            NSString *country = place.country;

            NSLog(@"name__%@, thoroughfare__%@, locality__%@, ISOcountryCode__%@, country__%@", name, thoroughfare, locality, ISOcountryCode, country);

        }

        else

        {

            NSLog(@"geoCode Error______%@", error.description);

        }

    }];

}


//反地理编码 根据给定的经纬度,获得具体的位置信息

- (IBAction)reverseGeocode:(id)sender

{

    //34.71210500 113.75394900

    

    //根据经纬度信息构建location对象

    CLLocation *location = [[CLLocation alloc] initWithLatitude:34.71210500 longitude:113.75394900];

    [_geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        NSLog(@"placemarks___%@", placemarks);

        

    }];

}



#pragma mark - CLLocationManagerDelegate

//更新位置

- (void)locationManager:(CLLocationManager *)manager

didUpdateLocations:(NSArray *)locations

{

    //取出最后一个

    CLLocation *location = [locations lastObject];

    

    //位置信息经度和纬度

    CLLocationCoordinate2D coordinate = location.coordinate;

    //纬度  Degrees()

    CLLocationDegrees latitudeDegrees = coordinate.latitude;

    //经度

    CLLocationDegrees longitudeDegrees = coordinate.longitude;

    //37.332331  -122.031219

    NSLog(@"latitudeDegrees_____%f, longitudeDegrees____%f", latitudeDegrees, longitudeDegrees);

    

    //获取定位精度

    CLLocationAccuracy horizontalAccuracy = location.horizontalAccuracy;

    CLLocationAccuracy verticalAccuracy = location.verticalAccuracy;

    NSLog(@"horizontalAccuracy____%f, verticalAccuracy____%f", horizontalAccuracy, verticalAccuracy);

    

    //海拔高度

    CLLocationDistance altitude = location.altitude;

    NSLog(@"altitude___%f", altitude);

    

    //取得事件时间

    NSDate *eventDate = location.timestamp;

    //计算时间间隔如果eventDate比现在早返回值是负数

    NSTimeInterval timeInterval = [eventDate timeIntervalSinceNow];

    NSLog(@"timeInterval___%f", timeInterval);

    //判断获的的位置的时效性 abs()绝对值

    if (abs(timeInterval) < 15.0)

    {

        NSLog(@"longitudeDegrees____%.3f, latitudeDegrees____%.6f", longitudeDegrees, latitudeDegrees);

        

        NSString *locationString = [NSString stringWithFormat:@"longitudeDegrees____%.3f, latitudeDegrees____%.6f", longitudeDegrees, latitudeDegrees];

        _locationLabel.text = locationString;

    }

}


//在有一个新的方向时调用

- (void)locationManager:(CLLocationManager *)manager

       didUpdateHeading:(CLHeading *)newHeading

{

    //地磁方向取值范围 0.0 - 359.9 0时为地磁北极

    CLLocationDirection magneticHeading = newHeading.magneticHeading;

    //地理方向取值范围 0.0 - 359.9 0时为地理北极

    CLLocationDirection trueHeading = newHeading.trueHeading;

    //方向精度

    CLLocationDirection headingAccuracy = newHeading.headingAccuracy;

    NSLog(@"magneticHeading___%f, trueHeading___%f, headingAccuracy___%f", magneticHeading, trueHeading, headingAccuracy);

}


//更新位置遇到错误

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    //错误在可查看CLError.h

    NSLog(@"error_____%d____%@", error.code, error.description);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值