iOS 地图与定位开发系列教程(一)

iPhone SDK提供了三个类来管理位置信息:CLLocation CLLocationManager 和 CLLHeading(不常用)。除了使用GPS来获取当前的位置信息外,iPhone也可以基于WiFi基站和无线发射塔来获得位置信息。GPS的精度最高,可以精确到米级别,但是也最耗电。

1、CLLocation

CLLocation类代表一个位置信息,其中还包括了方向和速度。比如我在长安街188号以5公里/小时的速度往西走。CLLocation具有下面的属性和方法:

@property  CLLocationCoordinate2D coordinate; //以经度和纬度表示的位置信息
@property CLLocationDistance altitude;  //海拔
@property CLLocationAccuracy horizontalAccuracy; //水平精度(如:精确到米)
@property CLLocationAccuracy verticalAccuracy; //垂直精度
@property CLLocationDirection course; //方向
@property CLLocationSpeed speed; //速度
-(NSDate *)timeStamp;
-(CLLocationDistance)distanceFromLocation:(CLLocation *)location;//两个位置之间的距离

2、CLLocationManager

CLLocationManager类管理和提供位置服务。它的属性和方法有:

@property CLLocation *location; //位置
@property id<CLLocationManagerDelegate> delegate;
@property CLLocationDistance distanceFilter; //距离过滤,比如:500以内
@property CLlocationAccuracy verticalAccuracy; //垂直精度
-(void) startUpdatingLocation; //开始更新位置(比如:你在往某个地方走)
-(void)stopUpdatingLocation; //停止更新位置
-(void)startUpdatingHeading; //开始更新方向(比如:你改往东走)
-(void)stopUpdatingHeading; //停止更新方向

CLLocationManagerDelegate是一个委托类。你的应用程序需要使用这个委托类。

//当用户改变位置的时候,CLLocationManager回调的方法
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

//当用户改变方向的时候,所调用的方法
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLLHeading *)newHeading;

//当iPhone无法获得当前位置的信息时,所回调的方法是
-(void)locationManager: (CLLocationManager *)manager didFailLoadWithError:(NSError *)error;

posted on 2012-10-29 15:56  Solitary  阅读(261)  评论(0编辑  收藏  举报

导航