2013-04-24 99 views
0

在我的位置應用中執行didUpdateToLocation方法。此方法每秒調用一次並提供位置數據。但是我不需要每秒鐘獲取位置,我只需要每5分鐘啓動一次該方法。是否有可能做到這一點?每5分鐘取GPS位置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //initialize location lisetener on Application startup 
    self.myLocationManager = [[CLLocationManager alloc]init]; 
    self.myLocationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    self.myLocationManager.delegate = self; 
    [self.myLocationManager startUpdatingLocation]; 


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     self.viewController = [[INNOViewController alloc] initWithNibName:@"INNOViewController_iPhone" bundle:nil]; 
    } else { 
     self.viewController = [[INNOViewController alloc] initWithNibName:@"INNOViewController_iPad" bundle:nil]; 
    } 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 


-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    /* 
    if(self.executingInBackground) 
    { 
     NSLog(@"Aplication running in background"); 
    } 
    else 
    { 
     NSLog(@"Aplication NOT running in background"); 
    } 
    */ 


    //NSLog(@"new location->%@ and old location -> %@",newLocation,oldLocation); 

    NSString *urlAsString = @"http://www.apple.com"; 

    NSURL *url=[NSURL URLWithString:urlAsString]; 

    NSURLRequest *urlRequest=[NSURLRequest requestWithURL:url]; 

    NSOperationQueue *queue = [[NSOperationQueue alloc]init]; 


    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

     if ([data length] > 0 && error == nil) { 
      NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
      NSLog(@"Downloaded html -> %@",html); 
      //NSLog(@"Downloaded successfully"); 
     } 
     else if([data length] == 0 && error == nil) 
     { 
      NSLog(@"Nothing downloaded"); 
     } 
     else 
     { 
      NSLog(@"Error occured -> %@",error); 
     } 

    }]; 
} 
+1

如果您確實想每5分鐘獲取一次,那麼您可能應該使用計時器。用5分鐘的倒數計時器啓動計時器,獲得最準確的位置,然後再次啓動並重復執行此操作。 – 2013-04-24 08:37:43

+0

如果初始化位置管理器對象並開始在定時器內部偵聽,那麼如果應用程序轉到後臺,didUpdateToLocation事件是否會觸發? – Ramprasad 2013-04-24 09:08:19

+1

後臺中的應用程序未處於活動狀態,因此不會觸發。或者,您可以請求操作系統在後臺模式下工作,但除非您的應用程序屬於某種類型,例如VOIP或導航應用程序,否則您只能在有限的時間內授予此權限。那些有背景特權。 – 2013-04-24 09:13:47

回答

2

減少accuraccy和距離過濾器,這樣會減少在該方法被調用
頻率如果你想讓它5分鐘後叫那麼你可以強制調用的方法stopupdating和startupdating每五分鐘

3

因爲您要求kCLLocationAccuracyBest,所以很快就會這麼做。退後一點。它不是基於時間,而是基於三角距離。在這種精確度下,即使距離發生微小變化,也會在接收良好的地區觸發更新。使用不同的值。

此外,這些方法並不打算基於時間使用。它們意味着根據增量距離使用。

1

相反的:

[self.myLocationManager startUpdatingLocation]; 

使用:

[self.myLocationManager startMonitoringSignificantLocationChanges]; 

如果你使用:startUpdatingLocation它會調用每個第二委託方法。當您使用startMonitoringSignificantLocationChanges時,它會在發生重大位置變化或間隔5分鐘後調用委託方法。


startMonitoringSignificantLocationChanges

Starts the generation of updates based on significant location changes. - (void)startMonitoringSignificantLocationChanges

Discussion

This method initiates the delivery of location events asynchronously, returning shortly after you call it. Location events are delivered to your delegate’s locationManager:didUpdateLocations: method. The first event to be delivered is usually the most recently cached location event (if any) but may be a newer event in some circumstances. Obtaining a current location fix may take several additional seconds, so be sure to check the timestamps on the location events in your delegate method.

After returning a current location fix, the receiver generates update events only when a significant change in the user’s location is detected. For example, it might generate a new event when the device becomes associated with a different cell tower. It does not rely on the value in the distanceFilter property to generate events. Calling this method several times in succession does not automatically result in new events being generated. Calling stopMonitoringSignificantLocationChanges in between, however, does cause a new initial event to be sent the next time you call this method.

If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives. In such a case, the options dictionary passed to the locationManager:didUpdateLocations: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.

In addition to your delegate object implementing the locationManager:didUpdateLocations: method, it should also implement the locationManager:didFailWithError: method to respond to potential errors.

Note: Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

Declared In CLLocationManager.h

參考CLLocationManager

+0

「不希望每五分鐘更頻繁地發出一次通知」與「每五分鐘一次呼叫」不一樣 – borrrden 2013-04-24 08:46:22

+0

即使我處於相同位置,我也需要每5分鐘發一次事件 – Ramprasad 2013-04-24 09:09:50

+1

@Ram然後我建議您編寫你自己的方法,每5分鐘在一個計時器上調用。無論何時您的位置發生變化,只需將其存儲在某個地方,然後在您的方法被調用時使用它。不要指望能夠在後臺做到這一點。 – borrrden 2013-04-24 09:28:38

1

你可以使用startMonitoringSignificantLocationChanges代替startUpdatingLocation。只有當用戶從最後位置移動大約500米時纔會更新。