Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

varchar

macrumors member
Original poster
Aug 9, 2008
62
0
Hi,

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")];
}
else
{
// 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];


}
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
You are in violation of the NDA that you agreed to when you downloaded the iPhone SDK.
 

varchar

macrumors member
Original poster
Aug 9, 2008
62
0
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...
 

mspatts

macrumors newbie
Aug 28, 2008
8
0
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...

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.
 

varchar

macrumors member
Original poster
Aug 9, 2008
62
0
FYI.

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.

Code:
  // 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];
    }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.