您的位置:首页 > 移动开发 > IOS开发

IOS开发—系统定位

2015-10-06 21:05 501 查看

说明

涉及到的框架核心定位框架CoreLocation,需要导入头文件

#import <CoreLocation/CoreLocation.h>


主要涉及的类有定位管理器CLLocationManager、位置类CALocation、地理信息编码类CLGeocoder、地标类CLPlacemark 以及 协议CLLocationManagerDelegate。

CLLocationManager 用于设置定位信息,比如精度等。

CALocation 记录经纬度等信息

CLGeocoder 相当于一个地址簿,保存了庞大的地址数据,用于地理编码(具体地址名称->地标)或反地理编码(CALocation->地标)

CLPlacemark 地标类,封装了经纬度,国家,城市等地址信息

CLLocationManagerDelegate 用于相关回调

代码示例

- (void)viewDidLoad {
[super viewDidLoad];

//初始化定位管理器
self.locationManager = [[CLLocationManager alloc]init];
//设置代理
self.locationManager.delegate = self;
//定位精准度
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//横向移动多少距离后更新位置信息
self.locationManager.distanceFilter = 10;
//ios8以后需要获取定位权限
//有两个方法,取决于你的定位使用情况 一个是requestAlwaysAuthorization(始终允许获取定位信息),一个是requestWhenInUseAuthorization(使用期间允许获取定位信息)
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
//开始定位
[self.locationManager startUpdatingLocation];
}


#pragma mark - CLLocationManagerDelegate
//位置更新时的回调 改方法存在时下面回调这种方法失效
//- (void)locationManager:(CLLocationManager *)manager
//     didUpdateLocations:(NSArray *)locations{
//
//    CLLocation *location = [locations lastObject];
//}

//位置更新时的回调
//若同时存在- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations回调,那么本方法失效;
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
CLLocationCoordinate2D loc = newLocation.coordinate;
self.labelLatitude.text = [NSString stringWithFormat:@"%f",loc.latitude];
self.labelLongitude.text = [NSString stringWithFormat:@"%f",loc.longitude];

// 获取当前所在的地址
//初始化地理信息编码类(CLGeocoder类相当于一个地址簿,保存了庞大的地址数据)
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//反地理编码
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){
//数组中包含了编译出的地标
if (array.count > 0){
//获取到编译出的地标(CLPlacemark是一个地标类,封装了经纬度,国家,城市等地址信息)
CLPlacemark *placemark = [array objectAtIndex:0];
//当前地址
self.labelAdress.text = placemark.name;
NSLog(@"placemark.name = %@",placemark.name);
//获取当前城市城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
NSLog(@"city = %@", city);

NSLog(@"addressDictionary = %@",placemark.addressDictionary);
//地址字典:(该字典属性打包了所有获取到的有效的地理信息)打出字典中的每个元素查看
/*
FormattedAddressLines = (中国浙江省杭州市西湖区留下街道)
Name = 中国浙江省杭州市西湖区留下街道
City = 杭州市
Country = 中国
State = 浙江省
SubLocality = 西湖区
CountryCode = CN
*/
NSLog(@"name = %@",placemark.name);
//定位到的详细地址:name = 中国浙江省杭州市西湖区留下街道
NSLog(@"thoroughfare = %@",placemark.thoroughfare);
//街道地址:thoroughfare = (null)
NSLog(@"subThoroughfare = %@",placemark.subThoroughfare);
//其他街道级地标的信息:subThoroughfare = (null)
NSLog(@"locality = %@",placemark.locality);
//城市名(对于直辖市,用administrativeArea):locality = 杭州市
NSLog(@"subLocality = %@",placemark.subLocality);
//其他城市级地标的信息:subLocality = 西湖区
NSLog(@"administrativeArea = %@",placemark.administrativeArea);
//行政区域:administrativeArea = 浙江省
NSLog(@"subAdministrativeArea = %@",placemark.subAdministrativeArea);
//其他行政区域坐标:subAdministrativeArea = (null)
NSLog(@"postalCode = %@",placemark.postalCode);
//邮编:postalCode = (null)
NSLog(@"ISOcountryCode = %@",placemark.ISOcountryCode);
//国家名缩写:ISOcountryCode = CN
NSLog(@"country = %@",placemark.country);
//国家:country = 中国
NSLog(@"inlandWater = %@",placemark.inlandWater);
//定位到的内陆水源名称:inlandWater = (null)
NSLog(@"ocean = %@",placemark.ocean);
//定位到的海洋:ocean = (null)
NSLog(@"areasOfInterest = %@",placemark.areasOfInterest);
//大的地标建筑:areasOfInterest = (null)

}

else if (error == nil && [array count] == 0)
{
NSLog(@"No results were returned.");
}
else if (error != nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
}

//定位失败时的回调
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSString *errMsg = nil;
if ([error code] == kCLErrorDenied) {
errMsg = @"访问被拒绝";
}
if ([error code] == kCLErrorLocationUnknown) {
errMsg = @"获取位置信息失败";
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"定位" message:errMsg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}




ios8中需要额外定位授权

1、添加授权代码

iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:

(1)始终允许访问位置信息

- (void)requestAlwaysAuthorization;

(2)使用应用程序期间允许访问位置数据

- (void)requestWhenInUseAuthorization;

具体代码已在上面贴出。

2、配置info.plist

在info.plist文件中添加两个参数NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription 参数的值自定义字符串,用于在授权定位服务提示框中显示,也可以不设值。



参考文档:

《iOS8中使用CoreLocation定位》

《IOS百度地图使用基础指南+原生分享&友盟分享》

《iOS定位获取当前所在经纬度》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios开发 系统定位