didUpdateToLocation未调用

时间:2017-11-12 13:28:29

标签: objective-c google-maps cllocationmanager

我有一个非常复杂的地理位置问题:没有调用didupdatetolocation委托方法,我不明白。 enter image description here 这个方法的代码:

 - (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];
    }

当我读到这个问题时,我发现这个方法已被弃用,但它仍在处理我的项目的旧版本,这是非常不正常的。 请帮忙。

4 个答案:

答案 0 :(得分:0)

确保您的CLLocationManager变量不是本地变量。在创建startUpdatingLocation后致电CLLocationManager。其中一个错误可能导致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];
    }

}

答案 1 :(得分:0)

是的我确信我在这个方法的代码中完成了这个,这个代码在完美运行时被调用

- (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];
        }
    }
}

答案 2 :(得分:0)

这是启动自动徒步旅行的方法:

- (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);
    }
}

答案 3 :(得分:0)

谢谢@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];


    }
}