iOS获取当前的位置
2015-07-31 17:35
405 查看
在iOS中获取当前的位置信息,包括 维度 经度 城市 街道 路口等信息
使用步骤:
创建CLLocationManager示例,并且需要强引用它
设置CLLocationManager的代理,监听并获取所更新的位置
启动位置更新
由于在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的代理,监听并获取所更新的位置
启动位置更新
由于在iOS8中,需要开发者主动向系统请求授权,所以在iOS8及以上系统中,需要以下步骤:
在info.plist文件中设置NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription
在代码中使用[_manager requestWhenInUseAuthorization]请求授权
实现Manager的代理方法didChangeAuthorizationStatus:,根据状态判断是否启动位置更新
参数分析
在Manager的代理方法locationManager: didUpdateLocations:中,其传入的locations参数是CLLocation类型。
CLLocation方法的主要参数:
2、方向
使用步骤
和定位一样的三个步骤,不同的是获取方向不需要授权
参数分析
在Manager的代理方法locationManager: didUpdateHeading:中,其传入的newHeading参数是CLHeading类型。
CLHeading方法的主要参数:
3、区域监听
使用步骤
也需要大致三个步骤,其中前两个步骤和定位一样,第三个步骤是创建一个范围:
代理方法(一进一出)
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;
}
}];
}
使用步骤:
创建CLLocationManager示例,并且需要强引用它
设置CLLocationManager的代理,监听并获取所更新的位置
启动位置更新
在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的代理,监听并获取所更新的位置
启动位置更新
在info.plist文件中设置NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription
在代码中使用[_manager requestWhenInUseAuthorization]请求授权
实现Manager的代理方法didChangeAuthorizationStatus:,根据状态判断是否启动位置更新
参数分析
在Manager的代理方法locationManager: didUpdateLocations:中,其传入的locations参数是CLLocation类型。
CLLocation方法的主要参数:
使用步骤
和定位一样的三个步骤,不同的是获取方向不需要授权
在Manager的代理方法locationManager: didUpdateHeading:中,其传入的newHeading参数是CLHeading类型。
CLHeading方法的主要参数:
使用步骤
也需要大致三个步骤,其中前两个步骤和定位一样,第三个步骤是创建一个范围:
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;
}
}];
}
相关文章推荐
- iOS开发new与alloc/init的区别
- ios开发学习1:开发的方法(转)
- iOS 锁屏强制切换屏幕的方向
- iOS 多线程NSThread
- iOS- 利用AFNetworking(AFN) - 实现图片上传 -
- 高德地图开发文档 iOS
- 27个提升效率的iOS开源库推荐
- iOS学习----------正则表达式
- iOS之访问权限
- iOS学习之内存管理
- iOS学习之解析XML数据
- 可变cell,自适应cell,理解iOS 8中的Self Sizing Cells和Dynamic Type
- iOS学习之多线程
- iOS开发——Xcode基本操作
- 斯坦福大学iOS8公开课笔记01-IOS8结构和MVC模式
- 关于iOS多线程,你看我就够了(已更新)
- iOS学习之KVO、KVC
- [转]关于iOS多线程,你看我就够了
- 关于iOS多线程,你看我就够了
- iOS开发Xcode之:iOS项目的完整重命名方法图文教程