I am trying to set up my MapView so that only some of the Pin Views will show CallOut Button. for my map view controller Maps.h I have:
Code:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@class DetailView;
@interface Maps : UIViewController <MKMapViewDelegate> {
DetailView *_detailView;
IBOutlet MKMapView *mapView;
NSMutableArray *annotations;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) NSMutableArray *annotations;
@property (retain) DetailView *detailView;
@end
The Maps.m
Code:
#import "Maps.h"
#import "DisplayMap.h"
#import "DetailView.h"
@implementation Maps
@synthesize annotations;
@synthesize detailView = _detailView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[mapView setMapType:MKMapTypeSatellite];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.delegate = self;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 32.385325 ;
region.center.longitude = -86.217442;
region.span.longitudeDelta = 0.005f;
region.span.latitudeDelta = 0.005f;
[mapView setRegion:region animated:YES];
CLLocationCoordinate2D theCoordinate;
self.annotations=[[NSMutableArray alloc] init];
//the first annotation
theCoordinate.latitude = 32.388152;
theCoordinate.longitude = -86.216948;
CLLocationCoordinate2D law;
//the first annotation
law.latitude = 32.383024;
law.longitude = -86.214974;
CLLocationCoordinate2D rotunda;
//the first annotation
rotunda.latitude = 32.384129;
rotunda.longitude = -86.216476;
CLLocationCoordinate2D margaret;
//the first annotation
margaret.latitude = 32.382363;
margaret.longitude = -86.220682;
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"Tennis Courts";
ann.subtitle = @"";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
DisplayMap *ann1 = [[DisplayMap alloc] init];
ann1.title = @"Harrison Field";
ann1.subtitle = @"Baseball";
ann1.coordinate = theCoordinate;
[mapView addAnnotation:ann1];
DisplayMap *ann2 = [[DisplayMap alloc] init];
ann2.title = @"Thomas Goode Jones School of Law";
ann2.subtitle = @"Marjorie Snook Building";
ann2.coordinate = law;
[mapView addAnnotation:ann2];
DisplayMap *ann3 = [[DisplayMap alloc] init];
ann3.title = @"E.L. Collum Rotunda";
ann3.subtitle = @"";
ann3.coordinate = rotunda;
[mapView addAnnotation:ann3];
DisplayMap *ann4 = [[DisplayMap alloc] init];
ann4.title = @"Margaret Harris";
ann4.subtitle = @"Women's Residence Hall";
ann4.coordinate = margaret;
[mapView addAnnotation:ann4];
[annotations addObject:ann3];
[annotations addObject:ann4];
[annotations addObject:ann2];
[annotations addObject:ann1];
[annotations addObject:ann];
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[detailButton setTitle:annotation.title forState:UIControlStateNormal];
NSInteger annotationValue = [self.annotations indexOfObject:annotation];
detailButton.tag = annotationValue;
[detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = detailButton;
return pinView;
}
else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
-(IBAction)showDetailView:(UIView*)sender {
NSString *where = ((UIButton*)sender).currentTitle;
DetailView *nextController = [[DetailView alloc] initWithNibName:@"DetailView" bundle:nil];
[self presentModalViewController:nextController animated:YES];
[nextController changeProductText:where];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
I have tried adding an NSString *callitem to the NSObject 'DisplayMaps' .h file, and then on the annotations, calling like this:
Code:
ann.callitem = @"LetCall";
and in the viewForAnnotation, changing to:
Code:
if (annotations.callitem isEqualToString:@"LetCall" {
...
}
However, I get the error Property for callitem not found on NSMutableArray.
Am I going about this the right way, or is there an easier (Or at least better way) to only allow some annotations to show callout?