CLLocationManager的例子,另外的一种实现,体现delegate做了什么工作。


想得到定点的信息,主要使用两个类,CLLocationManager,  CLLocationManagerdelegate协议,

m文件内容如下
1、先实例化一个CLLocationManager,同时设置委托及精确度等。

CCLocationManager *manager = [[CLLocationManager alloc] init];//初始化定位器
[manager setDelegate: self];//设置代理
[manager setDesiredAccuracy: kCLLocationAccuracyBest];//设置精确度
[manager startUpdatingLocation];//开启位置更新



2、接着实现CLLocationMangerDelegate协议的两个方法就可以了:

//定位时候调用
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation ;

//定位出错时被调
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
   
   
   
对应的h文件要写上遵守CLLocationManagerDelegate如下

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>    

@class CalibrationView, LevelView;

@interface LevelViewController : UIViewController <UIAccelerometerDelegate , CLLocationManagerDelegate> {
    
    CLLocationManager *locationManager;
}
@property(nonatomic, retain) CLLocationManager *locationManager;
@end


-----------------------------------



h文件也可以换一种写法,不写CLLocationManagerDelegate,但要写上CLLocationManagerDelegate里的两个方法。
对应的m文件内容通上,一样正常运行。delegate可以换成这样写,感觉不这么神秘了吧。

@interface LevelViewController : UIViewController <UIAccelerometerDelegate > {
    CLLocationManager *locationManager;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *) oldLocation ;
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *) error;

@end




你可能感兴趣的:(工作,manager,Class,interface,CLLocation)