tags:

views:

13

answers:

1

Hi guys,

I'm having trouble changing the pinColor for an annotation in MapKit. When I don't try to implement the mapView:viewForAnnotation: method everything works (the annotation is added) but when I try to change the annotation view, the simulator crashes :

Here is the code :

MapViewController.h

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"


@implementation MapViewController

@synthesize myMapView;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    CLLocationCoordinate2D location;
    location.longitude = 2.21;
    location.latitude = 48.5;
    MKCoordinateSpan span;
    span.latitudeDelta = 1*(1 - 0);
    span.longitudeDelta = 1*(1 - 0);
    MKCoordinateRegion region;
    region.span = span;
    region.center = location;
    [myMapView setRegion:region animated:NO];
    [myMapView regionThatFits:region];
    Annotation *someAnnotation =[[Annotation alloc] init];
    [myMapView addAnnotation:someAnnotation];
     }



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation {
    MKPinAnnotationView *customPinview = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
    customPinview.pinColor = MKPinAnnotationColorGreen;
    customPinview.animatesDrop = YES;
    customPinview.canShowCallout = YES;
    return customPinview;
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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


@end

MapViewController.m

//
//  MapViewController.h
//  TestMap
//
//  Created by Johan Ismael on 10/21/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

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


@interface MapViewController : UIViewController<MKMapViewDelegate> {

@private

    IBOutlet MKMapView *myMapView;

}

@property (nonatomic, retain) IBOutlet MKMapView *myMapView;
//- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation;

@end

Thanks in advance !!!

Note : MapViewController is declared as the delegate of the MapView in IB

A: 

In the viewForAnnotation method, the alloc is being done on MKAnnotationView instead of MKPinAnnotationView. It must be crashing with "unrecognized selector" because MKAnnotationView doesn't have a pinColor property. Change the alloc to:

MKPinAnnotationView *customPinview = [[[MKPinAnnotationView alloc] 
    initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
aBitObvious
Thank you ! and sorry for this pretty dumb question...:(
Johanisma