Lots of annotations!


#1

I have been spending a lot of time with Chapter 4 and 5 to make sure that I get how user driven MVC works. It’s a totally new paradigm for me. Additionally, I’ve never leveraged classes built by someone else. So far very exciting!

I have followed through chapters four and five, and I’ve got a pretty good grasp (still a little fuzzy) on what is happening, or suppose to be happening. However, when built and run on device, it’s… different than my expectations.

I’ll start out by saying that I’m using the 4.0 GM SDK, so maybe that’s a part of it…

When I take this app outside it gives the normal narrowing of Cellular to GPS blue fudge-factor circle, and then finally give just a blue flashing ball. However, it doesn’t hold GPS lock and will bounce back to various grain of GSP accuracy. I thought this may have something to do with starting and stopping the location manager; however the start and stop location manager methods aren’t even called until you return from text field. Hmm…

Also, maybe I’m not using the correct verb-age here, but after you annotate the map once (create a MapPoint which shows as a red pushpin on the map view) it continues to annotate with every change in GPS location. See screen shot:

What’s worse, is that the GPS updating and annotating means you cannot use the UITextField to add a string to the MapPoint.

So, I’m totally confused! Can anyone else confirm this behavior?

Here’s my code…

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	
	locationManager = [[CLLocationManager alloc] init];
	
	// Make this instance of WhereamiAppDelegate the delegate
	// locationManager will send its messages
	[locationManager setDelegate:self];
	
	// We want all results from the location manager
	[locationManager setDistanceFilter:kCLDistanceFilterNone];
	
	// We want it to be as accurate as possible
	// regardless of how much time/power it takes
	[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
	
	// Tell our manager to start looking for its location immediately
	// [locationManager startUpdatingLocation];
	[mapView setShowsUserLocation:YES];
	
    [window makeKeyAndVisible];
	
	return YES;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
	NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
	
	if (t < -180) {
		return;
	}
	
	MapPoint *mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate] andTitle:[locationTitleField text]];
	
	[mapView addAnnotation:mp];
	[mp release];
	
	[self foundLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
	NSLog(@"%@", error);
}

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
	MKAnnotationView *annotationView = [views objectAtIndex:0];
	id <MKAnnotation> mp = [annotationView annotation];
	MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250, 250);
	[mv setRegion:region animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)tf {
	[self findLocation];
	[tf resignFirstResponder];
	return (YES);
}

- (void)findLocation {
	[locationManager startUpdatingLocation];
	[activityIndicator startAnimating];
	[locationTitleField setHidden:YES];
}

- (void)foundLocation {
	[locationTitleField setText:@""];
	[activityIndicator stopAnimating];
	[locationTitleField setHidden:NO];
	[locationManager stopUpdatingHeading];
}

- (void)dealloc {
	[locationManager setDelegate:nil];
    [window release];
    [super dealloc];
}

#2

I was calling:

[locationManager stopUpdatingHeading];

in the foundLocation method
instead of:

[locationManager stopUpdatingLocation];

Man, so now I’ve gotta worry about my ducking iPhone auto-correcting and tab-completing the wrong method in Xcode. :laughing:

However, this doesn’t address the GPS accuracy. Any suggestions?


#3

I’m not sure what the question about GPS accuracy is? Do you mean to ask why does it bounce around a lot? If you want to fix that, change the distanceFilter of the locationManager.


#4

Joe, thanks for the super fast reply, but actually I think that this behavior is “typical” after testing a bit further in other apps

I am using kCLLocationAccuracyBest for setDesiredAccuracy on my CLLocationManager, and it gives me the same behavior as the Maps app.

So, I guess that’s just the maximum level of GPS detail to expect!