转:http://my.oschina.net/chengliqun/blog/147870
最近由于项目需要,需要使用LBS相关技术,但网上搜索了一圈,文章不是很多,故自己就从头整理记录些吧,以供日后温习参考;
下面就开始吧,首先,如标题,先定位吧。
第一步,新建一个singleView的空白工程,如果新建,这里不做赘述了。
第二步:因为地图开发相关的framework:MapKit.framework、CoreLocation.framework, 至于如何添加,一般的ios相关博客都是有介绍。
在主界面的控制器 ViewController.h 文件中,我们啥也不做,.m文件中,我们需声明一个 CLLocationManager* locationManager的属性,我们让其实现CLLocationManagerDelegate的协议,并覆写其更新位置的方法,如下:
// // ViewController.m // LBS_001_CLLocationManager // // Created by liqun on 13-7-17. // Copyright (c) 2013年 Block Cheng. All rights reserved. // #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate>{ } @property (nonatomic,retain)CLLocationManager* locationManager; @end @implementation ViewController -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { NSLog(@"nibName: %@ bundle: %@",nibBundleOrNil,nibBundleOrNil); _locationManager = [[CLLocationManager alloc] init]; } return self; } - (void)dealloc { self.locationManager = nil; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //delegate self.locationManager.delegate = self; //The desired location accuracy. self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; //Specifies the minimum update distance in meters. self.locationManager.distanceFilter = kCLDistanceFilterNone; self.locationManager.purpose = @"To provide functionality based on user's current location."; [self.locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { NSLog(@"didChangeAuthorizationStatus---%u",status); } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSLog(@"didChangeAuthorizationStatus----%@",error); } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ UIAlertView* av = [[UIAlertView alloc] initWithTitle:@"update" message:[NSString stringWithFormat:@"didUpdateToLocation: newLocation: %@ old:%@",newLocation,oldLocation] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; [av show]; [av release]; } - (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; } @end
点击好同意后,便能得到相应的位置信息,在代理的回调方法中,弹出信息
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
效果如下:
其实,在给出获取位置信息前,我们应该给出用户以提示的,见上效果图:
self.locationManager.purpose = @"To provide functionality based on user's current location.";
还有一种开去位置服务的方法,先检查位置服务是否可用,再获取:
if ([CLLocationManager locationServicesEnabled]){ self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.purpose = @"To provide functionality based on user's current location."; [self.locationManager startUpdatingLocation]; } else { /* Location services are not enabled. Take appropriate action: for instance, prompt the user to enable location services */ NSLog(@"Location services are not enabled"); }