天天看點

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. // This protocol is used to send the info for location updates back to another view controller  
  5. @protocol MyCLControllerDelegate <NSObject>  
  6. @required  
  7. -(void)gpsUpdate:(CLLocation *)aLoc;  
  8. @end  
  9. // Class definition  
  10. @interface MyCLController : NSObject <CLLocationManagerDelegate> {  
  11.     CLLocationManager *locationManager;  
  12.     CLLocation *myCurrentLoc;  
  13.     BOOL findShouldStop;  
  14.     id delegate;  
  15. }  
  16. @property (nonatomic, retain) CLLocationManager *locationManager;  
  17. @property (nonatomic,assign) id <MyCLControllerDelegate> delegate;  
  18. @property (nonatomic, assign) CLLocation *myCurrentLoc;  
  19. - (void)locationManager:(CLLocationManager *)manager  
  20.     didUpdateToLocation:(CLLocation *)newLocation  
  21.            fromLocation:(CLLocation *)oldLocation;  
  22. - (void)locationManager:(CLLocationManager *)manager  
  23.        didFailWithError:(NSError *)error;  
  24. + (MyCLController *)sharedInstance;  
  25. @end  

MyCLController.m

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