If your application isn't zooming


#1

The delegate method called after finding the user’s location has changed in recent version’s of iOS.

You must also implement this method in WhereamiAppDelegate.m:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
}

#2

Thank you very much!!!

i try it and worldView in undeclared so i change it with mapView, and give me this warning “local declaration of ‘mapView’ hides instance variable”, but now the zoom work!!! how i can fix it?

i wanna know another thing, can you post the spoiler for the challenge 1 and challenge 2 of the chapter 27 core data?..thank you very much…


#3

Hi

It’s just a warning saying that because the method argument mapView has the same name as the IBOutlet declared in the .h file you can’t access the IBOutlet from within this method.

You can get rid of the warning by giving the method argument a unique name.

- (void)mapView:(MKMapView *)worldView didUpdateUserLocation:(MKUserLocation *)userLocation { CLLocationCoordinate2D loc = [userLocation coordinate]; MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250); [worldView setRegion:region animated:YES]; }

mapView and worldView are both pointing to the same object so it makes no odds to the functionality.

Gareth


#4

Thanks you very much…i have a problem in Chapter 27: Core Data…i post a topic for the problem…if can u help me please…

thanks

Piero


#5

Well, my application still isn’t zooming, not sure if it has anything to do with the fact that I have upgraded to iOS 4.3.1… Running on an iPhone 4 and it does find my location except it is not zooming at all.

Here is the code:

[code]#import “WhereamiAppDelegate.h”

@implementation WhereamiAppDelegate

@synthesize window;

#pragma mark -
#pragma mark Application lifecycle

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    //Create location manager object
    locationManager = [[CLLocationManager alloc] init];

    // Make this instance of WhereamiAPPDelegate the delegate! (self refers to Whereami here)
    [locationManager setDelegate:self];

    // We want all the results from the location manager
    [locationManager setDistanceFilter:kCLDistanceFilterNone];

    // We want it to be as accurate as possible regardless of time and battery power used
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    // And tell our manager to start looking for its location immediately
    // Manager operates on a separate thread
    //[locationManager startUpdatingLocation];

    // Tell MKMapview to show current location
    [mapView setShowsUserLocation:YES];

    // Start updating heading information from the location manager
    //[locationManager startUpdatingHeading];

    [self.window makeKeyAndVisible];

    return YES;
    }

// Implement delegate method to start receiving CLLocation instances from the CLLocationManager

  • (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@“New Location is: %@”, newLocation);
    }

// Need to know if CLLocationManager fails to find a location and why

  • (void)locationManager:(CLLocationManager *)manager
    didFailWithError:(NSError *)error
    {
    NSLog(@“Could not find location: %@”, error);
    }

//Implement the delegate method for heading information

  • (void)locationManager:(CLLocationManager *)manager
    didUpdateHeading:(CLHeading *)newHeading
    {
    NSLog(@“New Heading is: %@”, newHeading);
    }

// Implement zooming on whenever an annotation is added to the map

  • (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
    {
    NSLog(@“Map zooming?”);
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate],250,250);
    [mv setRegion:region animated:YES];
    }

// Need to add this after iOS 4.2 or else zooming doesn’t work

  • (void)mapView:(MKMapView *)worldView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    NSLog(@“Erm… zooming yet?”);
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region2 = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region2 animated:YES];
    }

  • (void)applicationWillResignActive:(UIApplication )application {
    /

    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    */
    }

  • (void)applicationDidEnterBackground:(UIApplication )application {
    /

    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
    */
    }

  • (void)applicationWillEnterForeground:(UIApplication )application {
    /

    Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
    */
    }

  • (void)applicationDidBecomeActive:(UIApplication )application {
    /

    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    */
    }

  • (void)applicationWillTerminate:(UIApplication )application {
    /

    Called when the application is about to terminate.
    See also applicationDidEnterBackground:.
    */
    }

#pragma mark -
#pragma mark Memory management

  • (void)applicationDidReceiveMemoryWarning:(UIApplication )application {
    /

    Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
    */
    }

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

@end
[/code]


#6

Do you see the “Erm… zooming yet?” log statement? If not, make sure the delegate outlet of the MKMapView in the XIB file is connected to the WhereamiAppDelegate.


#7

Thank you kindly Joe, I had forgotten to set the delegate outlet of the MKMapView in the XIB file… I can confirm that this now works perfectly! :slight_smile:


#8

Ok, I’ve followed this thread and I still don’t see a zoom. I’m on XCode 4.0.2.

Seems like I have done the steps outlined above, but nothing is working for it.

These are my two defined functions in the .m file.

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

  • (void)mapView:(MKMapView *)worldView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
    }

I’ve set breakpoints in each and it appears they are called (as far as I understand this debugger so far).
This is output in debug window:
Pending breakpoint 1 - ““WhereamiAppDelegate.m”:104” resolved
Pending breakpoint 2 - ““WhereamiAppDelegate.m”:96” resolved

Any suggestions on where else I might look?

Thanks in advance!


#9

Hi,

The pending breakpoint resolved messages don’t mean that this code has executed - it’s just the debugger saying that it’s found their place in memory.

I would put the NSLog messages in - NSLog(@“Erm… zooming yet?”); to see if they are actually getting run.

If they are then you’ll see that message in the debug window.

Gareth


#10

Thanks for the reply. It was as the other person had trouble. The output linkage was missed. I guess this wasn’t exactly clear to me from the text since it’s all in a diagram. One of the challenges of doing this via the Kindle version of the book. (Small images.)

Thanks again!


#11

I hadn’t made all the outlet connections – missed the delegate outlet of the MKMapView back to WhereamiAppDelegate. I’m glad Joe [thanks!] mentioned this in his reply.
Perhaps a short section in each chapter of the next version of the book with “helpful hints” or “common problems” would be in order?


#12

Had the same problem: app wasn’t zooming…
I used the code JoeConway posted and then changed the method arg name from mapView to worldView as GarethR suggested.
problem solved!! :mrgreen:
This forum is fantastic - thanks for the help!


#13

Hi, I tried to follow the book as closely as posssible, and I’ve already linked all the worldView, textField and activityindicator together in the .xib file.

However, when I start to run the app, it crashes with either a “exc_bad_access” error, or it’ll throw a “unrecognized selector” exception.

I’ve been working on this for 4 hours now, but still don’t understand what’s wrong. Please help…

[code]#import “AppDelegate.h”
#import “ViewController.h”

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

  • (void)dealloc
    {
    if ([locationManager delegate] == self)
    {
    [locationManager setDelegate:nil];
    }
    [locationManager release];
    [_window release];
    [_viewController release];
    [super dealloc];
    }

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    locationManager = [[CLLocationManager alloc] init];

    [locationManager setDelegate:self];

    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    [locationManager startUpdatingLocation];

    [worldView setDelegate:self];
    [worldView setShowsUserLocation:YES];

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@“ViewController” bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
    }

  • (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    NSLog(@“zooming yet?”);
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
    }
    [/code]

My IBOutlets are declared as:

IBOutlet MKMapView* worldView; IBOutlet UITextField* locationField; IBOutlet UIActivityIndicatorView* rotater;

the NSLog is not showing the “zooming yet” part, which means that the program hasn’t even reached that part yet.