Ios中的定位功能是通過 Core Location框架實現的。它和地圖開發框架是相互獨立的。在Core Location中主要實現了定位和地理編碼的功能! 下麵我們就來介紹一下它的屬性,方法和代理方法! 屬性: desiredAccuracy:定位精度,是一個枚舉類型 //kCLLocationA
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(0, 0, 375, 122)]; label.font = [UIFont systemFontOfSize:16]; [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/
版權聲明:本文版權歸煙台傑瑞教育科技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
技術咨詢: