Annotation doesn't feel like showing up


#1

I can enter the annotation fine, but when I press “Done” and the text field disappears, I click on the blue dot and all I get us “Current Location”–no annotation. I checked the code of the other guy with this problem and the coding error he made I haven’t. Any thoughts on what I’m doing wrong?

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]

WhereAmIViewController.m

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

@implementation WhereAmIViewController

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

-(void)findLocation
{
[locationManager startUpdatingLocation];
[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)
    {
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    //[locationManager setDistanceFilter:50];
    //[CLLocationManager headingAvailable];
    //NSLog(@"%@",[locationManager heading]);
    }
    return self;
    }

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

[textField resignFirstResponder];
return YES;

}

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

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@",newLocation);
NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
if (t<180)
{
return;
}
[self foundLocation:newLocation];
}

-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@“Couldn’t find location: %@”,error);
}
@end[/code]

BNRMapPoint.h:

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

@interface BNRMapPoint : NSObject
{
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)t;
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;

@end[/code]

BNRMapPoint.m:

[code]#import “BNRMapPoint.h”

@implementation BNRMapPoint

@synthesize coordinate;
@synthesize 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]

Delegates and outlets all seem to be assigned correctly in .xib.


#2

I figured it out!

In WhereAmIViewController.m, in the following code:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"%@",newLocation); NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow]; if (t<180) { return; } [self foundLocation:newLocation]; }

I typed t<180 instead of t<-180–so it wouldn’t work unless the user had changed location less than three minutes in the future.

I have to admit, a lot of this is still pretty hazy to me, especially the stuff about delegation, but I’ll plow on through and hope that understanding will come in its own good time…


#3

You have no idea how you helped me! Thank you!


#4

Thanks.
This helped.

I overlooked the “-” beside the 180.