CLLocationManager用法示例 定位

点击打开链接开心程序


MyCLController.h


  1. #import <CoreLocation/CoreLocation.h>  
  2. // This is hoping that in the future (beyond SDK 2.0) we can access SystemConfiguration info  
  3. #import <SystemConfiguration/SCNetworkConnection.h>  
  4.   
  5. // This protocol is used to send the info for location updates back to another view controller  
  6. @protocol MyCLControllerDelegate <NSObject>  
  7. @required  
  8. -(void)gpsUpdate:(CLLocation *)aLoc;  
  9. @end  
  10.   
  11.   
  12. // Class definition  
  13. @interface MyCLController : NSObject <CLLocationManagerDelegate> {  
  14.     CLLocationManager *locationManager;  
  15.     CLLocation *myCurrentLoc;  
  16.     BOOL findShouldStop;  
  17.     id delegate;  
  18. }  
  19.   
  20. @property (nonatomic, retain) CLLocationManager *locationManager;  
  21. @property (nonatomic,assign) id <MyCLControllerDelegate> delegate;  
  22. @property (nonatomic, assign) CLLocation *myCurrentLoc;  
  23.   
  24. - (void)locationManager:(CLLocationManager *)manager  
  25.     didUpdateToLocation:(CLLocation *)newLocation  
  26.            fromLocation:(CLLocation *)oldLocation;  
  27.   
  28. - (void)locationManager:(CLLocationManager *)manager  
  29.        didFailWithError:(NSError *)error;  
  30.   
  31. + (MyCLController *)sharedInstance;  
  32.   
  33. @end  

MyCLController.m
  1. #import "MyCLController.h"  
  2.   
  3. // This is a singleton class, see below  
  4. static MyCLController *sharedCLDelegate = nil;  
  5.   
  6. @implementation MyCLController  
  7.   
  8. @synthesize delegate, locationManager, myCurrentLoc;  
  9.   
  10. - (id) init {  
  11.       
  12.     self = [super init];  
  13.     if (self != nil) {  
  14.         self.locationManager = [[CLLocationManager alloc] init];  
  15.         self.locationManager.delegate = self; // Tells the location manager to send updates to this object  
  16.         self.myCurrentLoc = [[CLLocation alloc] initWithLatitude:0 longitude:0];  
  17.         [self.locationManager startUpdatingLocation];  
  18.     }  
  19.     return self;  
  20. }  
  21.   
  22. // Called when the location is updated  
  23. - (void)locationManager:(CLLocationManager *)manager  
  24.     didUpdateToLocation:(CLLocation *)newLocation  
  25.            fromLocation:(CLLocation *)oldLocation  
  26. {     
  27.           
  28.     // Horizontal coordinates  
  29.     if (signbit(newLocation.horizontalAccuracy)) {  
  30.         // Negative accuracy means an invalid or unavailable measurement  
  31.         [self.delegate gpsUpdate:nil];  
  32.         return;  
  33.     }  
  34.       
  35.     //  // Altitude (we don't care about it)  
  36.     //  if (signbit(newLocation.verticalAccuracy)) {  
  37.     //      // Negative accuracy means an invalid or unavailable measurement  
  38.     //  }  
  39.       
  40.     // Check the timestamp, see if it's an hour old or more. If so, don't send an update  
  41.     if (ABS([newLocation.timestamp timeIntervalSinceNow]) > 3600) {  
  42.         [self.delegate gpsUpdate:nil];  
  43.         return;  
  44.     }  
  45.       
  46.     [myCurrentLoc release];  
  47.     myCurrentLoc = [newLocation copy]; // TODO: Why does this increment the retain count? We should be manually retaining  
  48.       
  49.     // Looks like the loc is good  
  50.     [self.delegate gpsUpdate:myCurrentLoc];  
  51.     return;  
  52. }  
  53.   
  54. // Called when there is an error getting the location  
  55. // TODO: Update this function to return the proper info in the proper UI fields  
  56. - (void)locationManager:(CLLocationManager *)manager  
  57.        didFailWithError:(NSError *)error  
  58. {  
  59.       
  60.     NSMutableString *errorString = [[[NSMutableString alloc] init] autorelease];  
  61.     BOOL shouldQuit;  
  62.       
  63.     if ([error domain] == kCLErrorDomain) {  
  64.           
  65.         // We handle CoreLocation-related errors here  
  66.           
  67.         switch ([error code]) {  
  68.                 // This error code is usually returned whenever user taps "Don't Allow" in response to  
  69.                 // being told your app wants to access the current location. Once this happens, you cannot  
  70.                 // attempt to get the location again until the app has quit and relaunched.  
  71.                 //  
  72.                 // "Don't Allow" on two successive app launches is the same as saying "never allow". The user  
  73.                 // can reset this for all apps by going to Settings > General > Reset > Reset Location Warnings.  
  74.                 //  
  75.             case kCLErrorDenied:  
  76.                 [errorString appendFormat:@"%@\n", NSLocalizedString(@"LocationDenied", nil)];  
  77.                 [errorString appendFormat:@"%@\n", NSLocalizedString(@"AppWillQuit", nil)];  
  78.                 shouldQuit = YES;  
  79.                 break;  
  80.                   
  81.                 // This error code is usually returned whenever the device has no data or WiFi connectivity,  
  82.                 // or when the location cannot be determined for some other reason.  
  83.                 //  
  84.                 // CoreLocation will keep trying, so you can keep waiting, or prompt the user.  
  85.                 //  
  86.             case kCLErrorLocationUnknown:  
  87.                 [errorString appendFormat:@"%@\n", NSLocalizedString(@"LocationUnknown", nil)];  
  88.                 [errorString appendFormat:@"%@\n", NSLocalizedString(@"AppWillQuit", nil)];  
  89.                 shouldQuit = YES;  
  90.                 break;  
  91.                   
  92.                 // We shouldn't ever get an unknown error code, but just in case...  
  93.                 //  
  94.             default:  
  95.                 [errorString appendFormat:@"%@ %d\n", NSLocalizedString(@"GenericLocationError", nil), [error code]];  
  96.                 shouldQuit = NO;  
  97.                 break;  
  98.         }  
  99.     } else {  
  100.         // We handle all non-CoreLocation errors here  
  101.         // (we depend on localizedDescription for localization)  
  102.         [errorString appendFormat:@"Error domain: \"%@\"  Error code: %d\n", [error domain], [error code]];  
  103.         [errorString appendFormat:@"Description: \"%@\"\n", [error localizedDescription]];  
  104.         shouldQuit = NO;  
  105.     }  
  106.       
  107.     // TODO: Send the delegate the alert?  
  108.     if (shouldQuit) {  
  109.         // do nothing  
  110.     }  
  111. }  
  112.   
  113. #pragma mark ---- singleton object methods ----  
  114.   
  115. // See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info  
  116.   
  117. + (MyCLController *)sharedInstance {  
  118.     @synchronized(self) {  
  119.         if (sharedCLDelegate == nil) {  
  120.             [[self alloc] init]; // assignment not done here  
  121.         }  
  122.     }  
  123.     return sharedCLDelegate;  
  124. }  
  125.   
  126. + (id)allocWithZone:(NSZone *)zone {  
  127.     @synchronized(self) {  
  128.         if (sharedCLDelegate == nil) {  
  129.             sharedCLDelegate = [super allocWithZone:zone];  
  130.             return sharedCLDelegate;  // assignment and return on first allocation  
  131.         }  
  132.     }  
  133.     return nil; // on subsequent allocation attempts return nil  
  134. }  
  135.   
  136. - (id)copyWithZone:(NSZone *)zone  
  137. {  
  138.     return self;  
  139. }  
  140.   
  141. - (id)retain {  
  142.     return self;  
  143. }  
  144.   
  145. - (unsigned)retainCount {  
  146.     return UINT_MAX;  // denotes an object that cannot be released  
  147. }  
  148.   
  149. - (void)release {  
  150.     //do nothing  
  151. }  
  152.   
  153. - (id)autorelease {  
  154.     return self;  
  155. }  
  156.   
  157. @end  


在用MKReverseGeocoder的时候是不是经常crash呀,那是因为在同一时刻只能存在一个MKReverseGeocoder实例。

参考资料:

http://evilrockhopper.com/2010/01/iphone-development-reverse-geocoding/

http://www.iphonedevsdk.com/forum/iphone-sdk-development/31883-pbrequestererrordomain-errors-reverse-geocoding.html





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值