【发布时间】:2011-11-21 14:44:19
【问题描述】:
当应用程序位于后台时,我无法发送我的位置。我正在使用 CLLocationManager 和 startMonitoringSignificantLocationChanges。位置 didUpdateToLocation 委托方法执行一次,但不会更多。我试着四处走走,但没有新的位置发送到服务器。
我在 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];
}
【问题讨论】:
-
你走了多远?根据您所在的位置,您可能需要走更远的距离
-
嗯,不远了..我会尝试通过走得更远来测试它。 1公里左右就足够了?
-
也许只是将
startMonitoringSignificantLocationChanges换成startUpdatingLocation看看它是否按预期运行......然后你将排除其他地方的问题 -
根据我的阅读,一个很大的依赖是手机信号塔的密度,所以如果你在市区,1公里可能就足够了,但如果你在更偏远的地区,这可能还不够
-
好的,我会尝试走更长的时间......
标签: iphone ios cllocationmanager