iOS获取当前的位置

在iOS中获取当前的位置信息,包括 维度 经度 城市 街道 路口等信息

使用步骤:

  • 创建CLLocationManager示例,并且需要强引用它

  • 设置CLLocationManager的代理,监听并获取所更新的位置

  • 启动位置更新

1
2
3
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
[_manager startUpdatingLocation];

由于在iOS8中,需要开发者主动向系统请求授权,所以在iOS8及以上系统中,需要以下步骤:

  • 在info.plist文件中设置NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription  配对YES

  • 在代码中使用[_manager requestWhenInUseAuthorization]请求授权

  • 实现Manager的代理方法didChangeAuthorizationStatus:,根据状态判断是否启动位置更新


首先 ,导入导入CoreLocation.frameWork

头文件包含#import<CoreLocation/CoreLocation.h>

并遵守<CLLocationManagerDelegate>代理协议

在interface中声明属性

//位置管理

@property(nonatomic,strong)CLLocationManager *currentLoaction;

//经度

@property(nonatomic,assign)CLLocationDegrees longitude;

//维度

@property(nonatomic,assign)CLLocationDegrees latitude;

//所在城市

@property(nonatomic,strong)NSString *city;


在实现中:

-(void)creatLocation{

    

    CLLocationManager *locationManager = [[CLLocationManageralloc] init];

    // 设置定位精度,十米,百米,最好

    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

    locationManager.delegate =self;

    self.currentLoaction = locationManager;

    if ([[UIDevicecurrentDevice].systemVersiondoubleValue] >= 8.0) {

        [locationManager requestAlwaysAuthorization];

    }

    [self.currentLoactionstartUpdatingLocation];

    //判断的手机的定位功能是否开启

    // 开启定位:设置 >隐私 > 位置 >定位服务

    if([CLLocationManagerlocationServicesEnabled]) {

        

        //启动位置更新

        //开启位置更新需要与服务器进行轮询所以会比较耗电,在不需要时用stopUpdatingLocation方法关闭;

        

    }else{

        

   //     UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"请开启定位功能"message:@"设置 >隐私 > 定位服务 >聚信" delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

        [alert show];

    }

}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

    if (status !=kCLAuthorizationStatusAuthorizedWhenInUse) {

        UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"请开启定位功能YES"message:@"设置 >隐私 > 定位服务 >聚信" delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

        [alert show];

    }

}

1.定位

使用步骤:

  • 创建CLLocationManager示例,并且需要强引用它

  • 设置CLLocationManager的代理,监听并获取所更新的位置

  • 启动位置更新

1
2
3
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
[_manager startUpdatingLocation];

由于在iOS8中,需要开发者主动向系统请求授权,所以在iOS8及以上系统中,需要以下步骤:

  • 在info.plist文件中设置NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription


  • 在代码中使用[_manager requestWhenInUseAuthorization]请求授权

  • 实现Manager的代理方法didChangeAuthorizationStatus:,根据状态判断是否启动位置更新

参数分析

在Manager的代理方法locationManager: didUpdateLocations:中,其传入的locations参数是CLLocation类型。

CLLocation方法的主要参数:

1
2
3
4
5
6
7
8
//经纬度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
//海平面
@property(readonly, nonatomic) CLLocationDistance altitude;
//速度
@property(readonly, nonatomic) CLLocationSpeed speed
//当前时间戳
@property(readonly, nonatomic, copy) NSDate *timestamp;

2、方向

使用步骤

和定位一样的三个步骤,不同的是获取方向不需要授权

1
2
3
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
[_manager startUpdatingHeading];

参数分析

在Manager的代理方法locationManager: didUpdateHeading:中,其传入的newHeading参数是CLHeading类型。

CLHeading方法的主要参数:

1
2
3
4
//与磁北方向的偏角
@property(readonly, nonatomic) CLLocationDirection magneticHeading;
//与正北方向的偏角
@property(readonly, nonatomic) CLLocationDirection trueHeading;

3、区域监听

使用步骤

也需要大致三个步骤,其中前两个步骤和定位一样,第三个步骤是创建一个范围:

1
2
3
4
5
6
7
8
9
10
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
 
if  ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
    [_manager requestAlwaysAuthorization];
}
 
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(32.656688, 110.74677);
CLCircularRegion *circular = [[CLCircularRegion alloc] initWithCenter:coordinate radius:1000 identifier:@ "bourne" ];
[_manager startMonitoringForRegion:circular];

代理方法(一进一出)

1
2
3
4
5
6
7
8
9
//进入范围时调用
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
     NSLog(@ "我进来了!" );
}
 
//离开范围时调用
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
     NSLog(@ "我出去了!" );
}

HELP:在iOS8.3中好像没作用,真机和模拟器都不行,iOS7.1正常工作!我也不知道怎么回事儿,如果有人知道希望能告诉我一下。谢谢。

4、 位置 代理方法:

// 6.0 以上调用这个函数

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    

    CLLocation *newLocation = [locationslastObject];

    CLLocationCoordinate2D newCoordinate = newLocation.coordinate;

    self.longitude = newCoordinate.longitude;

    self.latitude = newCoordinate.latitude;


//    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;

//    NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);

//

//        CLLocation *newLocation = locations[1];

//        CLLocationCoordinate2D newCoordinate = newLocation.coordinate;

//        NSLog(@"经度:%f,纬度:%f",newCoordinate.longitude,newCoordinate.latitude);

//

        计算两个坐标距离

//        float distance = [newLocation distanceFromLocation:oldLocation];

//        NSLog(@"%f",distance);

    

    [manager stopUpdatingLocation];

    

    //------------------位置反编码---5.0之后使用-----------------

    CLGeocoder *geocoder = [[CLGeocoderalloc]init];

    [geocoder reverseGeocodeLocation:newLocationcompletionHandler:^(NSArray *placemarks,NSError *error){

                       

       for (CLPlacemark *placein placemarks) {

//           NSLog(@"name,%@",place.name);                       //位置名

//           NSLog(@"thoroughfare,%@",place.thoroughfare);       //街道

//           NSLog(@"subThoroughfare,%@",place.subThoroughfare); //子街道

//           NSLog(@"locality,%@",place.locality);               //

//           NSLog(@"subLocality,%@",place.subLocality);         //

//           NSLog(@"country,%@",place.country);                 //国家

           self.city = place.locality;

           break;

       }

   }];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值