CLManager - slow and not accurate

Discussion in 'iOS Programming' started by varchar, Sep 1, 2008.

  1. varchar macrumors member

    Aug 9, 2008

    I am successfully using the CLLocationManager (and associated objects) to interact with the GPS. It seems to work "ok", but the main problem I am having is that it does not seem to be updating properly.

    For example:

    I get GPS coordinates at one location. Then I walk about 1 block down and the coordinates remain the same (yes I am looking after the 3rd decimal point)

    Interesting enough, when I use the iphone maps app, it seems to update more frequently and accurately... Any suggestions?

    Below is my code (keep in mind I modified most of it from various sources, including the SDK examples).

    // Called when the location is updated
    - (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation

    NSMutableString *lastTimeUpdated = [[[NSMutableString alloc] init] autorelease];
    NSMutableString *currentLatitude = [[[NSMutableString alloc] init] autorelease];
    NSMutableString *currentLatitudeDirection = [[[NSMutableString alloc] init] autorelease];

    NSMutableString *currentLongitude = [[[NSMutableString alloc] init] autorelease];
    NSMutableString *currentLongitudeDirection = [[[NSMutableString alloc] init] autorelease];

    // Timestamp
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    [lastTimeUpdated appendFormat:mad:"%@\n\n", [dateFormatter stringFromDate:newLocation.timestamp]];

    // Horizontal coordinates
    if (signbit(newLocation.horizontalAccuracy))
    // Negative accuracy means an invalid or unavailable measurement
    [currentLatitude appendString:LocStr(@"LatLongUnavailable")];
    // CoreLocation returns positive for North & East, negative for South & West

    //currentLatitude = [NSString stringWithFormat:mad:"%lf",fabs(newLocation.coordinate.latitude)];
    currentLatitude = [NSString stringWithFormat:mad:"%lf",newLocation.coordinate.latitude];
    currentLatitudeDirection = signbit(newLocation.coordinate.latitude) ? LocStr(@"South") : LocStr(@"North");

    currentLongitude = [NSString stringWithFormat:mad:"%lf",fabs(newLocation.coordinate.longitude)];
    currentLongitude = [NSString stringWithFormat:mad:"%lf",newLocation.coordinate.longitude];

    currentLongitudeDirection = signbit(newLocation.coordinate.latitude) ? LocStr(@"West") : LocStr(@"East");

    // [update appendString:mad:"\n"];
    //[update appendFormat:LocStr(@"MeterAccuracyFormat"), newLocation.horizontalAccuracy];

    // Send the update to our delegate
    [self.delegate newLocationUpdate: lastTimeUpdated: currentLatitude: currentLatitudeDirection: currentLongitude: currentLongitudeDirection];

    -(void)newLocationUpdate: (NSString *)lastTimeUpdated: (NSString *) currentLatitude: (NSString *) currentLatitudeDirection: (NSString *) currentLongitude:(NSString *) currentLongitudeDirection {

    [activityIndicator startAnimating];

    myCurrentLatitude= currentLatitude;
    myCurrentLongitude = currentLongitude;

    [[MyCLController sharedInstance].locationManager stopUpdatingLocation];

  2. gnasher729 macrumors P6


    Nov 25, 2005
    You are in violation of the NDA that you agreed to when you downloaded the iPhone SDK.
  3. ploppy macrumors newbie

    Jan 13, 2007
    So what? So is everybody else on this forum. Nobody, Apple included, seems to care.

    Core Location is not updating because you're telling it not to.
  4. varchar thread starter macrumors member

    Aug 9, 2008
    I am stopping the update after I receive and update, and then starting it again...

    In either case, even when I take the stopupdating out it still gives a problem...
  5. varchar thread starter macrumors member

    Aug 9, 2008

    How so? Please explain... I would like to know... all I am doing is accessing the GPS coordinates.
  6. mspatts macrumors newbie

    Aug 28, 2008
    i'm not an expert but..

    i don't see you restarting GPS in your code, so assuming you are doing it elsewhere, i think it takes some time to start it up and get a new position. i would assume you only want to start it up once.

    and on the NDA, i don't think anyone is allowed to post any Apple code (even if you modified it).. although the NDA supposedly just changed.

    again, i'm not an expert on either of these issues.
  7. varchar thread starter macrumors member

    Aug 9, 2008

    I solved this issue... This is how:

    In the newLocationUpdate(), I ONLY take the coordinates if it is accurate enough and it is new enough.

      // Needed to filter cached and too old locations
        if ((!currentLocation || currentLocation.horizontalAccuracy > newLocation.horizontalAccuracy) &&
            (howRecent < -0.0 && howRecent > -10.0)) {
            if (currentLocation)
                [currentLocation release];
            currentLocation = [newLocation retain];

Share This Page