作者:朱克锋
转载请注明出处:http://blog.csdn.net/linux_zkf
1,使用定位系统时要注意下面的这几行代码
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LoncationViewController :
UIViewController <CLLocationManagerDelegate> {
}2,还要注意添加包:CoreLocation.framework
关键代码实例
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g\u00B0",
newLocation.coordinate.latitude];
NSLog(@"newLocation.coordinate.latitude : %@",latitudeString);
[latitudeString release];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ?
@"Access Denied" : @"Unknown Error";
NSLog(@"errorType : %@",errorType);