2010-11-04 111 views
3
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  




// Override point for customization after application launch. 

locmanager = [[CLLocationManager alloc] init]; 
[locmanager setDelegate:self]; 
[locmanager setDesiredAccuracy:kCLLocationAccuracyHundredMeters]; 
//[locmanager setDistanceFilter:10]; 
updateTimer = [NSTimer timerWithTimeInterval:600 target:self selector:@selector(startUpdating) userInfo:nil repeats:YES]; 
[[NSRunLoop mainRunLoop] addTimer:updateTimer forMode:NSDefaultRunLoopMode]; 

[window makeKeyAndVisible]; 

return YES; 
} 

-(void)startUpdating 
{ 
[locmanager startUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
     if (newLocation.horizontalAccuracy < 0) return; 


    CLLocationCoordinate2D loc = [newLocation coordinate]; 
currentdate=[[NSDate date]timeIntervalSince1970]; 
    latitude = [NSString stringWithFormat: @"%f", loc.latitude]; 
longitude= [NSString stringWithFormat: @"%f", loc.longitude]; 
//Call to webservice to send data 
} 

我想送座標Web服務每10 minutes.Tried這樣做,但這不是working.My申請註冊到拿到background.Please位置更新,建議我改變了需要做這個程序。發送數據每10分鐘

+0

*它不工作*是什麼意思?發佈任何錯誤消息或輸出。 – 2010-11-04 16:09:07

+0

提供更多信息。你期望發生什麼,實際發生了什麼? – Jasarien 2010-11-04 16:09:55

+0

我想每10分鐘向服務器發送一次GPS座標。 – agupta 2010-11-04 16:32:41

回答

5

我做用NSUserDefaults的記錄它最後發送到服務器的日期時間和使用NSTimeInterval更新的位置的時間戳和這個值之間進行比較,類似這樣的東西。我正在使用30秒,但可以向上調整。這有點破解,但它與後臺運行等

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
} 

    updatedLocation = [newLocation retain]; 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    NSDate *myDate = (NSDate *)[prefs objectForKey:@"myDateKey"]; 

    NSDate *lastDate = (NSDate *)newLocation.timestamp; 
    NSTimeInterval theDiff = [lastDate timeIntervalSinceDate:myDate]; 

    if (theDiff > 30.0f || myDate == nil){ 
      //do your webservices stuff here 
      NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
      [prefs setObject:lastDate forKey:@"myDateKey"]; 
      [prefs synchronize]; 

    } 
+0

這段代碼太棒了,我正在使用這個代碼。感謝Robert。 – agupta 2010-11-10 18:20:51

+0

我有一些問題可以使用,請你幫忙。 – agupta 2010-11-10 18:46:22

+0

羅伯特可以請你幫我這個。我有一些問題使用這個。 – agupta 2010-11-11 19:53:53

0

您需要停止位置管理器中的更新。它將繼續調用didUpdateToLocation,除非您在第一次返回後停止它。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
[manager stopUpdatingLocation]; 
etc... 
+0

如果我每隔10分鐘打電話給我的web服務一次,那麼該怎麼辦?如果我打電話給我的web服務,那麼每10分鐘一次就會打電話給我的web服務。這是一種正確的方法。這種方式我會在10分鐘內忽略所有的數據。我希望我的應用程序在後臺運行請提出一個正確的方法來做到這一點。 – agupta 2010-11-04 16:46:05

+0

這將工作,但當然會上傳每10分鐘相同的座標,而不是更新的座標。如果您想要更新的位置,您可以按照上面顯示的方式執行此操作。 – Ben 2010-11-04 17:09:21

+0

我做完[locmanager startUpdatingLocation]; – agupta 2010-11-04 17:16:57