我想知道是否有人能再帮我一次忙?我已经将我的位置编码从我的视图控制器移到了NSObject中。然后,我从App Delegate调用了它
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Updating Location
[Location sharedLocation];
//Timer for reloading the XML
recheckTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(recheckLocation) userInfo:nil repeats:YES];
return YES:
}我已经设置了一个计时器,当我想让这个进程再次运行时
-(void)recheckLocation
{
//Updating Location
[Location sharedLocation];
NSLog(@"Timer Triggered");
}唯一的问题是,当计时器触发时,共享位置不会再次更新??有没有人能提前帮我?非常感谢,乔恩。
#import "Location.h"
@implementation Location
@synthesize locationManager;
- (id)init {
self = [super init];
if(self) {
self.locationManager = [CLLocationManager new];
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:500];//Metres
[self.locationManager setHeadingFilter:90];
[self.locationManager startMonitoringSignificantLocationChanges];
}
return self;
}
+ (Location*)sharedLocation {
static Location* sharedLocation;
if(!sharedLocation) {
@synchronized(sharedLocation) {
sharedLocation = [Location new];
}
}
return sharedLocation;
}
//LOCATION CODING
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError' %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Failed to Get Your Current Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
//Resolve Web Address
webAddressResolved = [NSString stringWithFormat:@"XZYYFASDFA%f,%f.xml", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
NSLog(@"Address Resolved %@", webAddressResolved);
}
//Stop Location Manager
[locationManager stopMonitoringSignificantLocationChanges];
}发布于 2013-04-02 14:43:34
您在这里定义的是位置的单例,而共享位置的目的似乎并不像代码注释中所说的那样更新位置
sharedInstance所做的是返回一个引用,该引用在初始化后在整个code.It中的任何地方都被使用。返回给您的是location实例,您没有在任何地方检索它,并且使用it.You只调用它而不使用它。
定义一个更新位置的方法,并在从该位置获取内存后调用该方法
Location *sharedLoc=[Location sharedLocation];并调用该方法将位置更新为
[sharedLoc updateLocation];Location.h
-(void)updateLocation;在位置.m
-(void)updateLocation
{
//Code for updation purpose
}https://stackoverflow.com/questions/15750431
复制相似问题