Quote:
Originally Posted by David Casseres
I haven't even reached the point of trying to do anything with callouts – all I do is create an instance of a very simple class that implements MKAnnotation, and add it to the map view using addAnnotation:. An exception is immediately thrown from from several frames down in the MapKit code, and it looks like an out-of-bounds index for an NSArray is causing it.
I've also tried using an MKPlacemark object (created by a reverse geocoder) instead of my own custom class, and exactly the same thing happens.
|
OK, here's my AnnotationListObject.h file:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface AnnotationListObject : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
- (void) moveAnnotation: (CLLocationCoordinate2D) newCoordinate;
@end
And the AnnotationListObject.m file:
#import "AnnotationListObject.h"
@implementation AnnotationListObject
@synthesize coordinate, title, subtitle;
- (void) moveAnnotation: (CLLocationCoordinate2D) newCoordinate {
coordinate = newCoordinate;
}
@end
and the relevant calls from RootViewController:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: [annotation title]];
if (pin == nil)
{
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: [annotation title]] autorelease];
}
else
{
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorGreen;
pin.animatesDrop = YES;
pin.canShowCallout = TRUE;
return pin;
}
- (void) doAnnotations { // call this from viewDidAppear or somesuch
NSMutableArray annotationList = [NSMutableArray new];
// create loop here if necessary
AnnotationListObject *newAnnotation = [AnnotationListObject new];
CLLocationCoordinate2D tempCoordinate;
tempCoordinate.latitude = 0.0; // set latitude to required value
tempCoordinate.longitude = 0.0; // set longitude to required value
[newAnnotation setCoordinate: tempCoordinate];
[newAnnotation setTitle: @"Title"]; // or whatever
[newAnnotation setSubtitle: @"Subtitle"]; // or whatever
[annotationList addObject: newAnnotation];
newAnnotation release];
// end loop here if looped
[map addAnnotations: annotationList];
}
this is all assuming you have an MKMapView called 'map' correctly connected in Interface Builder. Import the 'AnnotationViewList.h' in the RootViewController file and it should behave. It does for me. Now, if only I could get setRegion:animated: to behave...