How does the textfield get set before sending to MapKit?


#1

I am trying to finish CH5 whereami excercise. Running on iPhone simulator 4.3. Using the hackLocationFix.

When executes, map comes up, blue point zoomed over apple hq. I enter text into text box, hit done, then red pin shows up close to apple blue spot, but no annotation appears. In tracing execution, the text field is nil when assigned to MapKit, but the text does get passed into textFieldShouldReturn…

Not sure what is wrong. Below is code:

[code]//
// WhereamiAppDelegate.m
// Whereami
//
// Created by John Romano on 10/17/11.
// Copyright 2011 Tabs 6 Dig. All rights reserved.
//

#import “WhereamiAppDelegate.h”
#import “MapPoint.h”

@implementation CLLocationManager (TemporaryHack)

  • (void)hackLocationFix
    {
    NSLog(@“in temporary hack hackLocationFix”);
    CLLocation *location = [[CLLocation alloc] initWithLatitude:37.331689 longitude:-122.030731];
    //CLLocation *location = [[CLLocation alloc] initWithLatitude:42 longitude:-50];
    [[self delegate] locationManager:self didUpdateToLocation:location fromLocation:nil];
    }

  • (void)startUpdatingLocation
    {
    NSLog(@“in temporary hack startUpdatingLocation”);

    [self performSelector:@selector(hackLocationFix) withObject:nil afterDelay:0.1];
    }

@end

@implementation WhereamiAppDelegate

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

  • (void)foundLocation:(CLLocation *)loc
    {

    CLLocationCoordinate2D coord = [loc coordinate];

    //create instance of MapPoing with the current data
    MapPoint *mp = [[MapPoint alloc] initWithCoordinate:coord title:[locationTitleField text]];

    NSLog(@“in foundLocation%@”, [locationTitleField text]);

    //add it to the map view
    [worldView addAnnotation:mp];

    //MKMapView retains its annotaions, we can release
    [mp release];

    //xoom the region to this location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];

    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
    }

@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    NSLog(@“in application, didFinishLaunchingWithOptions”);

    //create loction manger object
    locationManager = [[CLLocationManager alloc] init];

    //there will be a warning from this line of code, ignore for now
    [locationManager setDelegate:self];

    //we want all results from the location manager
    [locationManager setDistanceFilter:kCLHeadingFilterNone];

    //and 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];
    [worldView setShowsUserLocation:YES];

    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@"%@",newLocation);

    //how many seconds ago was this new location created?
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

    //CLLocationManagers will return the last found location of the
    //device first, you don’t want that data in this case.
    //if this location was made more than 3 minutes ago, ignore it.
    if (t < -180) {
    //this is cached data, you don’t want it, keep looking
    NSLog(@“ignoring info, t < -180”);
    return;
    }
    [self foundLocation:newLocation];
    }

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

-(BOOL)textFieldShouldReturn:(UITextField *)tf
{
NSLog(@“in textFieldShouldReturn, %@”, tf);

//this method isn't implemented yet
[self findLocation];

[tf resignFirstResponder];

return YES;

}

-(void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)u
{
CLLocationCoordinate2D loc = [u coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[worldView setRegion:region 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, this method is called instead of applicationWillTerminate: when the user quits.
    */
    }

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

    Called as part of the 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.
    Save data if appropriate.
    See also applicationDidEnterBackground:.
    */
    }

  • (void)dealloc
    {
    [_window release];
    [super dealloc];
    }

@end
/code]


#2

Hi,

That sounds right. I have to tap the red pin to see the annotation view.

There is a way to get the annotation to display automatically.

Add this after adding the annotation in foundLocation
[worldView selectAnnotation:mp animated:NO];

(for some reason it doesn’t work if I set animated to YES)

HTH
Gareth


#3

Thanks Gareth. It helped me too.