Can't get annotations to work


#1

I’ve been able to get everything to work up until the end. I can get the location, zoom in, and submit text, but annotations won’t work. The text box just disappears and never returns and the activity monitor just keeps spinning endlessly. What am I doing wrong here?
WhereamiViewController.m

[code]#import “WhereamiViewController.h”
#import “BNRMapPoint.h”

@implementation WhereamiViewController

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

-(void)foundLocation:(CLLocation *)loc
{
CLLocationCoordinate2D coord = [loc coordinate];

BNRMapPoint *mp = [[BNRMapPoint alloc]initWithCoordinate:coord title:[locationTitleField text]];

[worldView addAnnotation:mp];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
[worldView setRegion:region animated:YES];

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

}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {
    //Create location manager object
    locationManager = [[CLLocationManager alloc]init];
    
    [locationManager setDelegate:self];
    
    //We want to be as accurate as possible, regardless of the amount of time/power it takes
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}

return self;

}

-(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];

if (t < -180) {
    return;
}

[self foundLocation:newLocation];

}

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

-(void)dealloc
{
//Tell the location manager to stop sending us messages
[locationManager setDelegate:nil];
}

-(void)viewDidLoad
{
[worldView setShowsUserLocation:YES];
}

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

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self findLocation];

[textField resignFirstResponder];

return YES;

}
@end
[/code]

WhereamiViewController.h

[code]#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>
{
CLLocationManager *locationManager;

IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;

}

-(void)findLocation;
-(void)foundLocation:(CLLocation *)loc;

@end
[/code]

BNRMapPoint.h

[code]#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface BNRMapPoint : NSObject
{

}
//A new designated initializer for instances of BNRMapPoint
-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;

//This is a required property from MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

//This is an optional property from MKAnnotation
@property (nonatomic, copy) NSString *title;

@end
[/code]

BNRMapPoint.m

[code]#import “BNRMapPoint.h”

@implementation BNRMapPoint
@synthesize coordinate, title;

-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
self = [super init];
if (self) {
coordinate = c;
[self setTitle:t];
}
return self;
}

-(id)init
{
return [self initWithCoordinate:CLLocationCoordinate2DMake(43.07, -89.32) title:@“Hometown”];
}
@end
[/code]


#2

Check your XIB file to make sure the delegate for the MKMapView is configured.


#3

The XIB appears to be fine with all of the connections made in the right direction.


#4

You accidentally stopped updating for the user’s location in - (void)findLocation, instead of startUpdatingLocation. See if that fixes it for you, or if you are still having a problem.


#5

I made the change and redid all of my connections. It’s still doing the exact same thing as before.


#6

Found the problem. It was a mixture of the issue you found and another similar to it. For the foundLocation method, I accidentally had it start updating location instead of stop. After fixing both, it works 100% correctly. Thanks for your help!