0

我只是想知道是否有人可以再次帮助我?我已将我的位置编码从我的视图控制器移动到一个 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];

}
4

1 回答 1

0

好吧,你在这里定义的是一个单独的位置,共享位置的目的似乎并没有像代码注释中所说的那样更新位置

sharedInstance 所做的是返回一个已初始化的引用,并且在整个代码中的任何地方都使用相同的引用。它为您提供位置实例作为回报,您不会在任何地方检索并使用它。您只需调用它但不使用它.

定义一个方法来更新位置并在使用从位置获取内存后调用它

Location *sharedLoc=[Location sharedLocation];

并调用方法将位置更新为

[sharedLoc updateLocation];

位置.h

-(void)updateLocation;

在位置 .m

-(void)updateLocation
{
//Code for updation purpose
}
于 2013-04-02T06:43:33.840 回答