2011-11-21 103 views
2

我在應用程序位於後臺時發送位置時遇到問題。我正在使用CLLocationManager和startMonitoringSignificantLocationChanges。 postion didUpdateToLocation委託方法執行一次,但不是更多。我試着走過去,但沒有新的位置被髮送到服務器。當iphone位於後臺時用CLLocationManager發送位置到服務器

我已經在info.plist文件中設置了「所需的背景模式」 - >「位置更新的應用程序註冊」。

任何人都知道什麼可能是錯的?從跟蹤被啓動,其中

代碼:

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = appDelegate; 
[appDelegate setLocationManager:locationManager withDistanceFilter:kCLDistanceFilterNone]; 
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges]; 

代碼(從CLLocationManagerDelegate):

- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter { 

    // create a new manager and start checking for sig changes 
    self.theLocationManager.delegate = nil; 
    [theLocationManager release]; 

    self.theLocationManager = locationManager; 
    self.theLocationManager.delegate = self; 
    self.theLocationManager.distanceFilter = distanceFilter; 
} 


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

     NSDate *newLocationTimestamp = newLocation.timestamp; 
     NSDate *oldLocationTimestamp = oldLocation.timestamp; 

     int locationUpdateInterval = 15;//15 sec 

     if (!([newLocationTimestamp timeIntervalSinceDate:oldLocationTimestamp] < locationUpdateInterval)) { 
       //NSLog(@"New Location: %@", newLocation); 
       [self updateToLocation:newLocation]; 
     } 

    } 

    - (void)updateToLocation:(CLLocation *)newLocation { 
     NSLog(@"update location!!"); 

     NSString *latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude]; 
     NSString *longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude]; 

     [currentUser updatePositionWithLongitude:longitude andLatitude:latitude]; 
    } 
+2

多遠你走動?根據您的位置,您可能需要覆蓋更遠距離 –

+0

hm,不遠處。我會嘗試通過進一步行走來測試它。 1公里左右應該足夠還是? – Madoc

+2

也許只是將'startMonitoringSignificantLocationChanges'替換爲'startUpdatingLocation'並查看它是否按預期運行......然後您將排除我讀過的其他地方的其他問題 –

回答

3

就像比爾·布拉斯基所說的,你設置位置管理器的準確性可能不會記錄你走過的距離。嘗試設置您的位置管理器的精度要高得多,只是爲了查看是否有效,然後將其撥回精確度和電池效率之間的中等程度。只是用於測試,把它所有的方式:

[appDelegate.theLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; 

然後代替:

[appDelegate.theLocationManager startMonitoringSignificantLocationChanges]; 

嘗試:

[appDelegate.theLocationManager startUpdatingLocation]; 
+0

謝謝,我現在使用startUpdatingLocation,它按預期工作。我仍然需要調整一下,因爲電池效率,但我會最終得到它:) – Madoc

+0

超級!很高興聽到這對你有用。 –

2

的-startMonitoringForSignificantLocationChanges是直接關係到手機信號塔的連接。您可能需要行駛里程才能連接到新塔樓並觸發位置更改事件。我知道區域監控更準確一些,因爲它使用Wifi,手機信號塔,甚至其他查詢位置的應用程序的位置更新。您需要弄清楚您的應用程序的準確性和頻率。您可能需要在後臺主動監控位置(肯定會成爲電池殺手)。希望這可以幫助。

+0

非常感謝你。 – Madoc

相關問題