我在應用程序位於後臺時發送位置時遇到問題。我正在使用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];
}
多遠你走動?根據您的位置,您可能需要覆蓋更遠距離 –
hm,不遠處。我會嘗試通過進一步行走來測試它。 1公里左右應該足夠還是? – Madoc
也許只是將'startMonitoringSignificantLocationChanges'替換爲'startUpdatingLocation'並查看它是否按預期運行......然後您將排除我讀過的其他地方的其他問題 –