
MyCLController.h
#@R_404_5565@ <CoreLocation/CoreLocation.h>// This is hoPing that in the future (beyond SDK 2.0) we can access SystemConfiguration info#@R_404_5565@ <SystemConfiguration/SCNetworkConnection.h>// This protocol is used to send the info for location updates back to another vIEw controller@protocol MyCLControllerDelegate <NSObject>@required-(voID)gpsUpdate:(CLLocation *)aLoc;@end// Class deFinition@interface MyCLController : NSObject <CLLocationManagerDelegate> { CLLocationManager *locationManager; CLLocation *myCurrentLoc; BOol findShouldStop; ID delegate;}@property (nonatomic,retain) CLLocationManager *locationManager;@property (nonatomic,assign) ID <MyCLControllerDelegate> delegate;@property (nonatomic,assign) CLLocation *myCurrentLoc;- (voID)locationManager:(CLLocationManager *)manager dIDUpdatetoLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;- (voID)locationManager:(CLLocationManager *)manager dIDFailWithError:(NSError *)error;+ (MyCLController *)sharedInstance;@end MyCLController.m
#@R_404_5565@ "MyCLController.h"// This is a singleton class,see belowstatic MyCLController *sharedCLDelegate = nil;@implementation MyCLController@synthesize delegate,locationManager,myCurrentLoc;- (ID) init { self = [super init]; if (self != nil) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; // Tells the location manager to send updates to this object self.myCurrentLoc = [[CLLocation alloc] initWithLatitude:0 longitude:0]; [self.locationManager startUpdatingLocation]; } return self;}// Called when the location is updated- (voID)locationManager:(CLLocationManager *)manager dIDUpdatetoLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ // Horizontal coordinates if (signbit(newLocation.horizontalAccuracy)) { // Negative accuracy means an invalID or unavailable measurement [self.delegate gpsUpdate:nil]; return; } // // Altitude (we don't care about it) // if (signbit(newLocation.verticalAccuracy)) { // // Negative accuracy means an invalID or unavailable measurement // } // Check the timestamp,see if it's an hour old or more. If so,don't send an update if (ABS([newLocation.timestamp timeIntervalSinceNow]) > 3600) { [self.delegate gpsUpdate:nil]; return; } [myCurrentLoc release]; myCurrentLoc = [newLocation copy]; // Todo: Why does this increment the retain count? We should be manually retaining // Looks like the loc is good [self.delegate gpsUpdate:myCurrentLoc]; return;}// Called when there is an error getting the location// Todo: Update this function to return the proper info in the proper UI fIElds- (voID)locationManager:(CLLocationManager *)manager dIDFailWithError:(NSError *)error{ NSMutableString *errorString = [[[NSMutableString alloc] init] autorelease]; BOol shouldQuit; if ([error domain] == kCLErrorDomain) { // We handle CoreLocation-related errors here switch ([error code]) { // This error code is usually returned whenever user taps "Don't Allow" in response to // being told your app wants to access the current location. Once this happens,you cannot // attempt to get the location again until the app has quit and @R_970_4404@ed. // // "Don't Allow" on two successive app launches is the same as saying "never allow". The user // can reset this for all apps by going to Settings > General > reset > reset Location Warnings. // case kCLErrorDenIEd: [errorString appendFormat:@"%@\n",NSLocalizedString(@"LocationDenIEd",nil)]; [errorString appendFormat:@"%@\n",NSLocalizedString(@"AppWillQuit",nil)]; shouldQuit = YES; break; // This error code is usually returned whenever the device has no data or WiFi connectivity,// or when the location cannot be determined for some other reason. // // CoreLocation will keep trying,so you can keep waiting,or prompt the user. // case kCLErrorLocationUnkNown: [errorString appendFormat:@"%@\n",NSLocalizedString(@"LocationUnkNown",nil)]; shouldQuit = YES; break; // We shouldn't ever get an unkNown error code,but just in case... // default: [errorString appendFormat:@"%@ %d\n",NSLocalizedString(@"GenericLocationError",nil),[error code]]; shouldQuit = NO; break; } } else { // We handle all non-CoreLocation errors here // (we depend on localizedDescription for localization) [errorString appendFormat:@"Error domain: \"%@\" Error code: %d\n",[error domain],[error code]]; [errorString appendFormat:@"Description: \"%@\"\n",[error localizedDescription]]; shouldQuit = NO; } // Todo: Send the delegate the alert? if (shouldQuit) { // do nothing }}#pragma mark ---- singleton object methods ----// See "Creating a Singleton Instance" in the Cocoa Fundamentals GuIDe for more info+ (MyCLController *)sharedInstance { @synchronized(self) { if (sharedCLDelegate == nil) { [[self alloc] init]; // assignment not done here } } return sharedCLDelegate;}+ (ID)allocWithZone:(NSZone *)zone { @synchronized(self) { if (sharedCLDelegate == nil) { sharedCLDelegate = [super allocWithZone:zone]; return sharedCLDelegate; // assignment and return on first allocation } } return nil; // on subsequent allocation attempts return nil}- (ID)copyWithZone:(NSZone *)zone{ return self;}- (ID)retain { return self;}- (unsigned)retainCount { return UINT_MAX; // denotes an object that cannot be released}- (voID)release { //do nothing}- (ID)autorelease { return self;}@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
总结以上是内存溢出为你收集整理的CLLocationManager用法示例全部内容,希望文章能够帮你解决CLLocationManager用法示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)