Current Location Bug in Xcode 4.5

前端 未结 3 1941
鱼传尺愫
鱼传尺愫 2021-01-07 00:35

In Xcode 4.5 apple introduced apple new maps. My application heavliy requires map services. And I have noticed in my application it shows the wrong current location until yo

3条回答
  •  旧巷少年郎
    2021-01-07 01:40

    The old approach from apple docs seems still working in iOS6 (didn't notice this in my active app (it tracks user's route via gps))

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
        NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
        if (locationAge > 5.0) return;
        if (newLocation.horizontalAccuracy < 0) return; 
    
        // proceed with coords here
    }
    

    UPDATE from the discussion: Calculating total and current distance could be done like this (excluding some minor stuff):

    // somewhere at the top
    CLLocation* lastUsedLocation = nil; // last location used in calculation
    CLLocation* pointA = nil;  // start of the track
    double totalDistance = 0;  // total distance of track
    double currentDistance = 0; // distance between startTrack point and current location
    ...
    
    // when you start updating location:
    - (void) startTracking {
        lastUsedLocation = nil;
        pointA = nil;
        totalDistance = 0;
        currentDistance = 0;
        [locationManager startUpdatingLocation];
    }
    ...
    
    
    // location update callback
     - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
        NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
        if (locationAge > 5.0) return;  // filter cached
        if (newLocation.horizontalAccuracy < 0) return; // filter invalid
    
        if(!pointA) pointA = [newLocation retain]; 
    
        if(lastUsedLocation) 
        { 
            totalDistance += [newLocation distanceFromLocation:lastUsedLocation]; 
        } 
        currentDistance = [pointA distanceFromLocation:newLocation]; 
        [lastUsedLocation release]; 
        lastUsedLocation = [newLocation retain]; 
    }
    

    If you need the option to turn off background location on purpose you disable it manually like:

    - (void)applicationDidEnterBackground:(UIApplication *)application {
        if(backgroundLocationDisabled)
        {
            [locationManager stopUpdatingLocation];
            // additional stuff
        }
    }
    

提交回复
热议问题