CLLocationManager 位置定位
转:http://my.oschina.net/chengliqun/blog/147870
最近由于项目需要,需要使用LBS相关技术,但网上搜索了一圈,文章不是很多,故自己就从头整理记录些吧,以供日后温习参考;
下面就开始吧,首先,如标题,先定位吧。
第一步,新建一个singleView的空白工程,如果新建,这里不做赘述了。
第二步:因为地图开发相关的framework:MapKit.framework、CoreLocation.framework, 至于如何添加,一般的ios相关博客都是有介绍。
在主界面的控制器 ViewController.h 文件中,我们啥也不做,.m文件中,我们需声明一个 CLLocationManager* locationManager的属性,我们让其实现CLLocationManagerDelegate的协议,并覆写其更新位置的方法,如下:
1 // 2 // ViewController.m 3 // LBS_001_CLLocationManager 4 // 5 // Created by liqun on 13-7-17. 6 // Copyright (c) 2013年 Block Cheng. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import <CoreLocation/CoreLocation.h> 11 @interface ViewController ()<CLLocationManagerDelegate>{ 12 13 } 14 15 @property (nonatomic,retain)CLLocationManager* locationManager; 16 17 @end 18 19 @implementation ViewController 20 21 -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 22 23 { 24 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 25 NSLog(@"nibName: %@ bundle: %@",nibBundleOrNil,nibBundleOrNil); 26 _locationManager = [[CLLocationManager alloc] init]; 27 28 } 29 30 return self; 31 } 32 33 - (void)dealloc 34 { 35 self.locationManager = nil; 36 [super dealloc]; 37 } 38 39 - (void)viewDidLoad 40 { 41 [super viewDidLoad]; 42 // Do any additional setup after loading the view, typically from a nib. 43 //delegate 44 self.locationManager.delegate = self; 45 //The desired location accuracy. 46 self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 47 //Specifies the minimum update distance in meters. 48 49 self.locationManager.distanceFilter = kCLDistanceFilterNone; 50 51 self.locationManager.purpose = @"To provide functionality based on user's current location."; 52 53 [self.locationManager startUpdatingLocation]; 54 } 55 56 - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { 57 NSLog(@"didChangeAuthorizationStatus---%u",status); 58 } 59 60 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ 61 NSLog(@"didChangeAuthorizationStatus----%@",error); 62 } 63 64 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ 65 UIAlertView* av = [[UIAlertView alloc] initWithTitle:@"update" message:[NSString stringWithFormat:@"didUpdateToLocation: newLocation: %@ old:%@",newLocation,oldLocation] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; 66 [av show]; 67 [av release]; 68 } 69 70 - (void)didReceiveMemoryWarning{ 71 [super didReceiveMemoryWarning]; 72 } 73 74 @end
程序启动后,系统会进行授权询问,如下:
点击好同意后,便能得到相应的位置信息,在代理的回调方法中,弹出信息
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
效果如下:
其实,在给出获取位置信息前,我们应该给出用户以提示的,见上效果图:
1 self.locationManager.purpose = @"To provide functionality based on user's current location.";
如果用户拒绝后,我们应该做相应的提示处理,在代理方法中也需做相应的处理。
还有一种开去位置服务的方法,先检查位置服务是否可用,再获取:
1 if ([CLLocationManager locationServicesEnabled]){ 2 self.locationManager = [[CLLocationManager alloc] init]; 3 self.locationManager.delegate = self; 4 self.locationManager.purpose = @"To provide functionality based on user's current location."; 5 [self.locationManager startUpdatingLocation]; 6 } else { 7 /* Location services are not enabled. 8 Take appropriate action: for instance, prompt the user to enable location services */ 9 NSLog(@"Location services are not enabled"); 10 }



浙公网安备 33010602011771号