如何每15分钟找到当前位置

时间:2023-01-12 23:57:50

I started developing a project using ios-5 , i want to find current location in every 15 min.

我开始使用ios-5开发项目,我希望每15分钟找到一个当前位置。

this is my code

这是我的代码

- (void)applicationDidEnterBackground:(UIApplication *)application {

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSDate *newLocationTimestamp = newLocation.timestamp;
    NSDate *lastLocationUpdateTiemstamp;
    int locationUpdateInterval = 60; //1min

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if (userDefaults) {
        lastLocationUpdateTiemstamp = [userDefaults objectForKey:@"myevent"];
     if (!([newLocationTimestamp timeIntervalSinceDate:lastLocationUpdateTiemstamp] < locationUpdateInterval)) {
            NSLog(@"New Location: %@", newLocation);

            //[self locationManager:locationManager didUpdateToLocation:newLocation fromLocation:oldLocation];
            [userDefaults setObject:newLocationTimestamp forKey:@"myevent"];
        }
    }
}

The problem I am having is that this code is working in background but i got current location(here is newLocation) in every 5min not after 1min, even i set locationUpdateInterval = 60sec

我遇到的问题是这个代码在后台工作,但我在1分钟内每隔5分钟获得当前位置(这里是newLocation),即使我设置locationUpdateInterval = 60sec

I have tried everything but not getting exact solution. Thanks in advance!

我已经尝试了一切但没有得到确切的解决方案提前致谢!

1 个解决方案

#1


1  

The anecdotal evidence I've heard is that, in order to save battery life, CoreLocation updates are only fired periodically for an application that lives in the background.

我听到的轶事证据是,为了节省电池寿命,CoreLocation更新只针对后台应用程序定期启动。

There are a few related questions I've seen here regarding this subject, like this one.

关于这个问题,我在这里看到了一些相关的问题,就像这个问题一样。

Also, take a look at Apple's Location Awareness Programming Guide, specifically the "Starting the Significant-Change Location Service" section.

另外,请查看Apple的位置感知编程指南,特别是“启动重要更改位置服务”部分。

#1


1  

The anecdotal evidence I've heard is that, in order to save battery life, CoreLocation updates are only fired periodically for an application that lives in the background.

我听到的轶事证据是,为了节省电池寿命,CoreLocation更新只针对后台应用程序定期启动。

There are a few related questions I've seen here regarding this subject, like this one.

关于这个问题,我在这里看到了一些相关的问题,就像这个问题一样。

Also, take a look at Apple's Location Awareness Programming Guide, specifically the "Starting the Significant-Change Location Service" section.

另外,请查看Apple的位置感知编程指南,特别是“启动重要更改位置服务”部分。