Ios中的定位功能是通过 Core Location框架实现的。它和地图开发框架是相互独立的。在Core Location中主要实现了定位和地理编码的功能!
下面我们就来介绍一下它的属性,方法和代理方法!
属性:
desiredAccuracy:定位精度,是一个枚举类型
//kCLLocationAccuracyBest:最精确定位
//kCLLocationAccuracyNearestTenMeters:十米误差范围
//kCLLocationAccuracyHundredMeters:百米误差范围
//kCLLocationAccuracyKilometer:千米误差范围
//kCLLocationAccuracyThreeKilometers:三千米误差范围
distanceFilter: 位置信息更新最小距离,只有移动大于这个距离才更新位置信息,默认为kCLDistanceFilterNone:不进行距离限制
对象方法:
startUpdatingLocation:开始定位追踪
stopUpdatingLocation : 停止定位追踪
startUpdatingHeading:开始方向追踪
stopUpdatingHeading:停止方向追踪
startMonitoringForRegion : 开始对某个区域进行追踪
stopMonitoringForRegion : 停止对某区域进行追踪
requestAlwaysAuthorization : 请求获得应用一直使用定位服务授权,注意使用此方法前要在info.plist中配置NSLocationAlwaysUsageDescription
requestWhenInUseAuthorization : 请求获得应用使用时的定位服务授权,注意使用此方法前在要在info.plist中配置NSLocationWhenInUseUsageDescription
代理方法:
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations; 位置发生改变后执行
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading; 导航方向发生变化后执行
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region;进入某个区域
- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region;走出某个区域之后执行
开代码:
//
// ViewController.m
// location
//
// Created by jerehedu on 15/12/23.
// Copyright (c) 2015年 jerehedu. All rights reserved.
// #import "ViewController.h"
#import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate>{
CLLocationManager *manager;
CLGeocoder *geocoder; } @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
label.font = [UIFont systemFontOfSize:];
[self.view addSubview:label];
if ([CLLocationManager locationServicesEnabled]) {
NSLog(@"定位服务已经打开");
}
//如果没有授权则请求用户授权
if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
[manager requestWhenInUseAuthorization];
}else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
manager = [[CLLocationManager alloc]init];
[manager requestAlwaysAuthorization ];
[manager requestWhenInUseAuthorization];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;//精准度
// 定位精度,枚举类型:
// kCLLocationAccuracyBest:最精确定位
// CLLocationAccuracy kCLLocationAccuracyNearestTenMeters:十米误差范围
//kCLLocationAccuracyHundredMeters:百米误差范围
//kCLLocationAccuracyKilometer:千米误差范围
//kCLLocationAccuracyThreeKilometers:三千米误差范围
manager.distanceFilter = 1.0;//移动十米定位一次
// 位置信息更新最小距离,只有移动大于这个距离才更新位置信息,默认为kCLDistanceFilterNone:不进行距离限制
[manager startUpdatingLocation];
} // Do any additional setup after loading the view, typically from a nib.
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *loctaion = [locations firstObject];
NSLog(@"%f %f",loctaion.coordinate.latitude,loctaion.coordinate.longitude );
/*[geocoder reverseGeocodeLocation:loctaion completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark=[placemarks firstObject]; CLLocation *location=placemark.location;//位置
CLRegion *region=placemark.region;//区域
NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息
// CLPlacemark *placemark = [placemarks firstObject];
// placemark.addressDictionary
NSString *name=placemark.name;//地名
NSString *thoroughfare=placemark.thoroughfare;//街道
NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等
NSString *locality=placemark.locality; // 城市
NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑
NSString *administrativeArea=placemark.administrativeArea; // 州
NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息
NSString *postalCode=placemark.postalCode; //邮编
NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码
NSString *country=placemark.country; //国家
NSString *inlandWater=placemark.inlandWater; //水源、湖泊
NSString *ocean=placemark.ocean; // 海洋
NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标
}];*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
出处:http://www.cnblogs.com/jerehedu/
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询: