未调用 didUpdateToLocation
didUpdateToLocation not called
我有一个非常复杂的地理定位问题:没有调用 didupdatetolocation 委托方法,我不明白。
这是方法的代码:
- (void) locationManager:(CLLocationManager *)pLocationManager didUpdateToLocation:(CLLocation *)pNewLocation fromLocation:(CLLocation *)pOldLocation {
lGPSAccuracy = 0;
// Stop trace record when the GPS Signal is poor or null
if (pNewLocation.horizontalAccuracy > GPS_POOR_SIGNAL || pNewLocation.horizontalAccuracy < 0){ return;}
if (pNewLocation.horizontalAccuracy > pNewLocation.verticalAccuracy) {
lGPSAccuracy = pNewLocation.horizontalAccuracy;
} else {
lGPSAccuracy = pNewLocation.verticalAccuracy;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(traceRecorder:didUpdateToLocation:fromLocation:)]) {
[self.delegate traceRecorder:self didUpdateToLocation:pNewLocation fromLocation:pOldLocation];
}
CLLocationCoordinate2D lUserLocation = pNewLocation.coordinate;
Boolean lLatitudeOK = (self.mMapSouthWest.latitude < lUserLocation.latitude) && (lUserLocation.latitude < self.mMapNorthEast.latitude);
Boolean lLongitudeOK = (self.mMapSouthWest.longitude < lUserLocation.longitude) && (lUserLocation.longitude < self.mMapNorthEast.longitude);
//IF USER IS OUT OF THE MAP
if (self.delegate && [self.delegate respondsToSelector:@selector(userPositionOnMap:)]) {
if ( (!lLatitudeOK || !lLongitudeOK) ) {
[self.delegate userPositionOnMap:NO];
} else {
[self.delegate userPositionOnMap:YES];
}
}
if (![self isNewLocation:pNewLocation acceptableComparedToOldLocation:pOldLocation]) {
return;
}
[self treatLocation:pNewLocation];
}
当我读到这个问题时,我发现这个方法已被弃用,但它仍在我的项目的旧版本上工作,这是非常不正常的。
请帮忙。
确保您的 CLLocationManager
变量不是局部变量。并在创建CLLocationManager
后调用startUpdatingLocation
。这 2 个错误之一可能导致 didUpdateToLocation
未被调用。
试试下面我的代码
- (void) beginMonitoring {
if (TracageAutomatic) {
tracageAutomaticTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tracageAutomaticTimeCalled) userInfo:nil repeats:YES];
}
if (self.mLocationManager == nil) {
[self setMLocationManager: [[CLLocationManager alloc] init]];
[self.mLocationManager setDelegate:self];
[self.mLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[self.mLocationManager setDistanceFilter:2.0f]; //Minimum distance to move in meters before calling location update.
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
//TODO: restore for iOS8
[self.mLocationManager requestWhenInUseAuthorization];
}
}
[self.mLocationManager startUpdatingLocation];
if ([self.mLocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[self.mLocationManager setAllowsBackgroundLocationUpdates:YES];
}
}
是的,我确定我在 运行 中调用的这个方法的代码中做到了这一点
- (void) beginMonitoring {
if (TracageAutomatic) {
tracageAutomaticTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tracageAutomaticTimeCalled) userInfo:nil repeats:YES];
} else {
if (self.mLocationManager == nil) {
[self setMLocationManager: [[CLLocationManager alloc] init]];
[self.mLocationManager setDelegate:self];
[self.mLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[self.mLocationManager setDistanceFilter:2.0f]; //Minimum distance to move in meters before calling location update.
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
//TODO: restore for iOS8
[self.mLocationManager requestWhenInUseAuthorization];
}
}
[self.mLocationManager startUpdatingLocation];
if ([self.mLocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[self.mLocationManager setAllowsBackgroundLocationUpdates:YES];
}
}
}
这是启动自动远足的方法:
- (void) tracageAutomaticTimeCalled{
if (_mUserReachedTrackEnd){
[tracageAutomaticTimer invalidate];
tracageAutomaticTimer = nil;
}
[self treatLocation:[self.mTrackPoints objectAtIndex:tracageAutomaticCurrentIndex]];
tracageAutomaticCurrentIndex += TracageAutomoticPas;
if (tracageAutomaticCurrentIndex > [self.mTrackPoints count]){
tracageAutomaticCurrentIndex = (int)([self.mTrackPoints count]-1);
}
}
谢谢@truncdug。我通过更改已弃用的 didtoupdatelocations 方法解决了这个问题
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
if ([locations count]>0) {
CLLocation *pNewLocation = [locations firstObject];
CLLocation *pOldLocation = _currentLocation;
_currentLocation = pNewLocation;
lGPSAccuracy = 0;
// Stop trace record when the GPS Signal is poor or null
if (pNewLocation.horizontalAccuracy > GPS_POOR_SIGNAL || pNewLocation.horizontalAccuracy < 0){ return;}
if (pNewLocation.horizontalAccuracy > pNewLocation.verticalAccuracy) {
lGPSAccuracy = pNewLocation.horizontalAccuracy;
} else {
lGPSAccuracy = pNewLocation.verticalAccuracy;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(traceRecorder:didUpdateToLocation:fromLocation:)]) {
[self.delegate traceRecorder:self didUpdateToLocation:pNewLocation fromLocation:pOldLocation];
}
CLLocationCoordinate2D lUserLocation = pNewLocation.coordinate;
Boolean lLatitudeOK = (self.mMapSouthWest.latitude < lUserLocation.latitude) && (lUserLocation.latitude < self.mMapNorthEast.latitude);
Boolean lLongitudeOK = (self.mMapSouthWest.longitude < lUserLocation.longitude) && (lUserLocation.longitude < self.mMapNorthEast.longitude);
//IF USER IS OUT OF THE MAP
if (self.delegate && [self.delegate respondsToSelector:@selector(userPositionOnMap:)]) {
if ( (!lLatitudeOK || !lLongitudeOK) ) {
[self.delegate userPositionOnMap:NO];
} else {
[self.delegate userPositionOnMap:YES];
}
}
if (![self isNewLocation:pNewLocation acceptableComparedToOldLocation:pOldLocation]) {
return;
}
[self treatLocation:pNewLocation];
}
}
我有一个非常复杂的地理定位问题:没有调用 didupdatetolocation 委托方法,我不明白。
- (void) locationManager:(CLLocationManager *)pLocationManager didUpdateToLocation:(CLLocation *)pNewLocation fromLocation:(CLLocation *)pOldLocation {
lGPSAccuracy = 0;
// Stop trace record when the GPS Signal is poor or null
if (pNewLocation.horizontalAccuracy > GPS_POOR_SIGNAL || pNewLocation.horizontalAccuracy < 0){ return;}
if (pNewLocation.horizontalAccuracy > pNewLocation.verticalAccuracy) {
lGPSAccuracy = pNewLocation.horizontalAccuracy;
} else {
lGPSAccuracy = pNewLocation.verticalAccuracy;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(traceRecorder:didUpdateToLocation:fromLocation:)]) {
[self.delegate traceRecorder:self didUpdateToLocation:pNewLocation fromLocation:pOldLocation];
}
CLLocationCoordinate2D lUserLocation = pNewLocation.coordinate;
Boolean lLatitudeOK = (self.mMapSouthWest.latitude < lUserLocation.latitude) && (lUserLocation.latitude < self.mMapNorthEast.latitude);
Boolean lLongitudeOK = (self.mMapSouthWest.longitude < lUserLocation.longitude) && (lUserLocation.longitude < self.mMapNorthEast.longitude);
//IF USER IS OUT OF THE MAP
if (self.delegate && [self.delegate respondsToSelector:@selector(userPositionOnMap:)]) {
if ( (!lLatitudeOK || !lLongitudeOK) ) {
[self.delegate userPositionOnMap:NO];
} else {
[self.delegate userPositionOnMap:YES];
}
}
if (![self isNewLocation:pNewLocation acceptableComparedToOldLocation:pOldLocation]) {
return;
}
[self treatLocation:pNewLocation];
}
当我读到这个问题时,我发现这个方法已被弃用,但它仍在我的项目的旧版本上工作,这是非常不正常的。 请帮忙。
确保您的 CLLocationManager
变量不是局部变量。并在创建CLLocationManager
后调用startUpdatingLocation
。这 2 个错误之一可能导致 didUpdateToLocation
未被调用。
试试下面我的代码
- (void) beginMonitoring {
if (TracageAutomatic) {
tracageAutomaticTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tracageAutomaticTimeCalled) userInfo:nil repeats:YES];
}
if (self.mLocationManager == nil) {
[self setMLocationManager: [[CLLocationManager alloc] init]];
[self.mLocationManager setDelegate:self];
[self.mLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[self.mLocationManager setDistanceFilter:2.0f]; //Minimum distance to move in meters before calling location update.
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
//TODO: restore for iOS8
[self.mLocationManager requestWhenInUseAuthorization];
}
}
[self.mLocationManager startUpdatingLocation];
if ([self.mLocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[self.mLocationManager setAllowsBackgroundLocationUpdates:YES];
}
}
是的,我确定我在 运行 中调用的这个方法的代码中做到了这一点
- (void) beginMonitoring {
if (TracageAutomatic) {
tracageAutomaticTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tracageAutomaticTimeCalled) userInfo:nil repeats:YES];
} else {
if (self.mLocationManager == nil) {
[self setMLocationManager: [[CLLocationManager alloc] init]];
[self.mLocationManager setDelegate:self];
[self.mLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[self.mLocationManager setDistanceFilter:2.0f]; //Minimum distance to move in meters before calling location update.
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
//TODO: restore for iOS8
[self.mLocationManager requestWhenInUseAuthorization];
}
}
[self.mLocationManager startUpdatingLocation];
if ([self.mLocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[self.mLocationManager setAllowsBackgroundLocationUpdates:YES];
}
}
}
这是启动自动远足的方法:
- (void) tracageAutomaticTimeCalled{
if (_mUserReachedTrackEnd){
[tracageAutomaticTimer invalidate];
tracageAutomaticTimer = nil;
}
[self treatLocation:[self.mTrackPoints objectAtIndex:tracageAutomaticCurrentIndex]];
tracageAutomaticCurrentIndex += TracageAutomoticPas;
if (tracageAutomaticCurrentIndex > [self.mTrackPoints count]){
tracageAutomaticCurrentIndex = (int)([self.mTrackPoints count]-1);
}
}
谢谢@truncdug。我通过更改已弃用的 didtoupdatelocations 方法解决了这个问题
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
if ([locations count]>0) {
CLLocation *pNewLocation = [locations firstObject];
CLLocation *pOldLocation = _currentLocation;
_currentLocation = pNewLocation;
lGPSAccuracy = 0;
// Stop trace record when the GPS Signal is poor or null
if (pNewLocation.horizontalAccuracy > GPS_POOR_SIGNAL || pNewLocation.horizontalAccuracy < 0){ return;}
if (pNewLocation.horizontalAccuracy > pNewLocation.verticalAccuracy) {
lGPSAccuracy = pNewLocation.horizontalAccuracy;
} else {
lGPSAccuracy = pNewLocation.verticalAccuracy;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(traceRecorder:didUpdateToLocation:fromLocation:)]) {
[self.delegate traceRecorder:self didUpdateToLocation:pNewLocation fromLocation:pOldLocation];
}
CLLocationCoordinate2D lUserLocation = pNewLocation.coordinate;
Boolean lLatitudeOK = (self.mMapSouthWest.latitude < lUserLocation.latitude) && (lUserLocation.latitude < self.mMapNorthEast.latitude);
Boolean lLongitudeOK = (self.mMapSouthWest.longitude < lUserLocation.longitude) && (lUserLocation.longitude < self.mMapNorthEast.longitude);
//IF USER IS OUT OF THE MAP
if (self.delegate && [self.delegate respondsToSelector:@selector(userPositionOnMap:)]) {
if ( (!lLatitudeOK || !lLongitudeOK) ) {
[self.delegate userPositionOnMap:NO];
} else {
[self.delegate userPositionOnMap:YES];
}
}
if (![self isNewLocation:pNewLocation acceptableComparedToOldLocation:pOldLocation]) {
return;
}
[self treatLocation:pNewLocation];
}
}