Cant' get mapView:didAddAnnotationViews: to fire


#1

Hello everyone…hoping I can get some help here. I’m new to Objective-C and cocoa and was needing some input on why the delegate method I’m trying to use isn’t firing. I’m only using the simulator for testing, so maybe it has something to do with that, but I doubt it.

So the problem is that the mapView:didAddAnnotationViews: is not firing as I expected it to after going through the whereami project. Here’s a few of the code snippets:

…the mapView:didAddAnnotationViews: method with a simple log statement to see if it’s firing at all

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
	NSLog(@"didAddAnnotationViews");
}

…the locationManager:didUpdateToLocation:fromLocation method, which I’m assuming should be sending the message to fire the mapView:didAddAnnotationViews:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
	NSLog(@"didUpdateToLocation");
	NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
	//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
		return;
	}
	
	MapPoint *mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate] title:[locationTitleField text]];
	[mapView addAnnotation:mp];
	[mp release];
	
	[self foundLocation];
}

…here’s the MapPoint .h file

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MapPoint : NSObject <MKAnnotation> {
	NSString *title;
	CLLocationCoordinate2D coordinate;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;	

@end

…and the MapPoint .m file

#import "MapPoint.h"

@implementation MapPoint 
@synthesize coordinate, title;

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t {
	[super init];
	//coordinate ivar set directly in the book...not sure why except maybe since it's a c structure, it doesn't need any of the memory management facilities in the accessor
	coordinate = c;
	//it won't work at all tring to use the accessor...i think it has something to do with MKAnnotation not using a pointer for the coordinate it returns
	//[self setCoordinate:c];
	[self setTitle:t];
	return self;
}

- (void)dealloc {
	[title release];
	[super dealloc];
}

@end

I have many other NSLog statements that are showing up in the log, and it seems that the program is working as it should except for this annotation process. When the program starts, it zooms to the Apple HQ location with the text field waiting for input. When I type in the input and hit enter, nothing happens on the screen or in the log. I’ve redone the attachments in the Interface Builder a couple times to make sure I have the delegates and controller/view associations right. So I’m stuck. Any help would be MUCH appreciated =)


#2

Hi,

Try adding this method to your appDelegate (from http://forums.bignerdranch.com/viewtopic.php?f=47&t=563)

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

If it doesn’t work in the simulator you could hack it by adding

if(TARGET_IPHONE_SIMULATOR){
            [newLocation initWithLatitude:37.33168900 longitude:-122.03073100];
}

at the very beginning of the didUpdateToLocation method so that you can check that the annotation code is working.

Gareth


#3

hey gareth. thanks for that simulator code…it got the didAddAnnotationViews: method firing. apparently when the simulator is being used, since the location isn’t changing, the didAddAnnotationViews: delegate method doesn’t get called. i don’t understand that, but i’m very new to iphone programming, so i’m sure there is a reason =)

anyhow, “tricking” it into a new location got an annotation pin to hit the map. thanks again for the help.