Question

I am coding an application that uses a mapView in iOS. I am trying to color the annotations in the mutable array to purple and to add a disclosure button to each pin . The reason that I am using a mutable array is that I am going to retrieve many places from a DB to view each place on the map with a pin.

My code is :

    - (void)viewDidLoad{

[super viewDidLoad];
// Do any additional setup after loading the view.


//code of map

[mapMKMapView setMapType:MKMapTypeStandard];

[mapMKMapView setZoomEnabled:YES];

[mapMKMapView setScrollEnabled:YES];


MKCoordinateRegion newRegion = { {0.0, 0.0},{0.0, 0.0}};

newRegion.center.latitude = 12.968427;

newRegion.center.longitude = 44.997704;

newRegion.span.latitudeDelta = 0.004731;

newRegion.span.longitudeDelta = 0.006952;

[self.mapMKMapView setRegion:newRegion animated:YES];



//multiple annotations

NSMutableArray *locations = [[NSMutableArray alloc] init ];

CLLocationCoordinate2D loc;

annotationClass *myAnn;



//1st annotation

myAnn = [[annotationClass alloc] init];

loc.latitude = 12.968427;

loc.longitude = 44.997704;

myAnn.coordinate = loc;

myAnn.title = @"Nakheel2";

myAnn.subtitle = @"This Al-Nakheel 2 stage";

[locations addObject:myAnn];



//2nd annotaion

myAnn = [[annotationClass alloc] init];

loc.latitude = 12.971532;

loc.longitude = 44.998015;

myAnn.coordinate = loc;

myAnn.title = @"Nakheel21";

myAnn.subtitle = @"This Al-Nakheel 2 stage Hi";

[locations addObject:myAnn];



[self.mapMKMapView addAnnotations:locations];

}


//******

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)locations

{

MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:locations reuseIdentifier:@"current"];

MyPin.pinColor = MKPinAnnotationColorPurple;



UIButton *adverButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

[adverButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

MyPin.rightCalloutAccessoryView = adverButton;

MyPin.draggable = NO;

MyPin.highlighted = YES;

MyPin.animatesDrop  = TRUE;

MyPin.canShowCallout  = YES;



return MyPin;

}
Was it helpful?

Solution

The viewForAnnotation delegate method will be called by the map view automatically (you don't explicitly "run" it).

The most likely reason the map view is not calling it is that the map view's delegate is not set.

In the storyboard or xib, make sure the map view's delegate outlet is connected to the view controller (right-click or ctrl-click the map view and connect the delegate outlet to the view controller).

Or, you can add this line in the code in viewDidLoad before the setMapType:

//code of map
mapMKMapView.delegate = self;                 // <-- add this line
[mapMKMapView setMapType:MKMapTypeStandard];


An unrelated point:

Instead of using a custom action method for the button, I suggest using the map view's own calloutAccessoryControlTapped delegate method. In that method, you'll get direct access to the annotation object that was tapped (via view.annotation). Remove the addTarget and the button: method if you decide to use the delegate method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top