首页 > 代码库 > iOS Core Location 实现定位
iOS Core Location 实现定位
在iphone中可以用core location功能来实现地理定位,并可用mapkit 框架加载google地图。 想得到定点的信息,其实 不难,只需要涉及到几个类,CLLocationManager, CLLocation, CLLocationManagerdelegate协 议,CLLocationCoodinate2D, CLLocationDegrees。 <一>先实例化一个CLLocationManager,同时设置委托及精确度等。 CCLocationManager *manager = [[CLLocationManager alloc] init]; [manager setDelegate: self]; [manager setDesiredAccuracy: kCLLocationAccuracyBest]; 其中desiredAccuracy属性表示精确度,有利5种选择如下: desiredAccuracy属性 描述 kCLLocationAccuracyBest 精确度最佳 kCLLocationAccuracynearestTenMeters 精确度10m以内 kCLLocationAccuracyHundredMeters 精确度100m以内 kCLLocationAccuracyKilometer 精确度1000m以内 kCLLocationAccuracyThreeKilometers 精确度3000m以内 NOTE:精确度越高,用点越多,就要根据实际情况而定。 manager.distanceFilter = 250;这个表示在地图上每隔250m才更新一次定位信息。 [manager startUpdateLocation]; 启动定位器,如果不用的时候就必须调用stopUpdateLocation以关闭定位功能。 <二>CCLocation对像中包含着定点的相关信息数据。其属性主要包括coordinate, altitude,horizontalAccuracy,verticalAccuracy, timestamp等,分别如下: coordinate 用来存储地理位置的latitude和longitude,分别表示 纬度和经度,都是float类型.如可这 样: float latitude = location.coordinat.latitude; location是CCLocation的实例。 这里也把上面提到的CLLocationDegrees,它其实是一个double类型,在core Location框架中是用来储存 CLLocationCoordinate2D实例coordinate的latitude 和longitude, typedef double CLLocationDegrees; typedef struct {CLLocationDegrees latitude; CLLocationDegrees longitude} CLLocationCoordinate2D; altitude 表示位置的海拔高度,这个值是极不准确的。 horizontalAccuracy 表示水平准确度,这么理解,它是以coordinate为圆心的半径,返回的值越小,证明准确度越好,如果是负数,则表示core location定位失败。 verticalAccuracy表示垂直准确度,它的返回值与altitude相关,所以不准确。 Timestamp 返回的是定位时的时间,是NSDate类型。 <三>CLLocationMangerDelegate协议 我们只需实现两个方法就可以了,如下: - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation ; - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error; 上面第一个是定位时候回访调,后者定位出错时被调。 <四>现在可以去实现定位了: 新建一个view-based application模板的工程,假设项目名称为coreLocation.我们在contronller的头文件和源文件中的代码大概有如下: .h #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface CoreLocationViewController : UIViewController <CLLocationManagerDelegate>{ CLLocationManager *locManager; } @property (nonatomic, retain) CLLocationManager *locManager; @end .m #import "CoreLocationViewController.h" @implementation CoreLocationViewController @synthesize locManager; // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; locManager.desiredAccuracy = kCLLocationAccuracyBest; [locManager startUpdatingLocation]; [super viewDidLoad]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [locManager stopUpdatingLocation]; [locManager release]; [textView release]; [super dealloc]; } #pragma mark - #pragma mark CoreLocation Delegate Methods - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLLocationCoordinate2D locat = [newLocation coordinate]; float lattitude = locat.latitude; float longitude = locat.longitude; float horizon = newLocation.horizontalAccuracy; float vertical = newLocation.verticalAccuracy; NSString *strShow = [[NSString alloc] initWithFormat: @"currentpos: 经度=%f 维度=%f 水平准确读=%f 垂直准确度=%f ", lattitude, longitude, horizon, vertical]; UIAlertView *show = [[UIAlertView alloc] initWithTitle:@"coreLoacation" message:strShow delegate:nil cancelButtonTitle:@"i got it" otherButtonTitles:nil]; [show show]; [show release]; } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSString *errorMessage; if ([error code] == kCLErrorDenied){ errorMessage = @"你的访问被拒绝";} if ([error code] == kCLErrorLocationUnknown) { errorMessage = @"无法定位到你的位置!";} UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:errorMessage delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [alert release]; } @end
iOS Core Location 实现定位
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。