I have annotations on my mapview (self.friendsMapView) that should present a callout when tapped. Each annotation displays a title and a description (body). For some reason, no matter what I try, my callout will not show if my annotation is tapped. Am I missing something here?
A bit of background: I previously had two mapViews inside one ViewController (and, I had callouts working fine). However after I decided to remove one mapview (self.neighboursMapView) and its code, my callouts for self.friendsMapView seem to have stopped working.
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
friendsMapView.showsUserLocation = YES;
[friendsMapView setMapType:MKMapTypeStandard];
[friendsMapView setZoomEnabled:YES];
[friendsMapView setScrollEnabled:YES];
NSMutableDictionary *viewParamsFriend = [NSMutableDictionary new];
[viewParamsFriend setValue:@"accepted_friends" forKey:@"view_name"];
[DIOSView viewGet:viewParamsFriend success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.friendData = [responseObject mutableCopy];
int index = 0;
for (NSMutableDictionary *multiplelocationsFriend in self.friendData) {
NSString *location = multiplelocationsFriend[@"address2"];
NSString *userNames = multiplelocationsFriend[@"first name"];
NSString *userBio = multiplelocationsFriend[@"body"];
CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
[geocoderFriend geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.friendsMapView.region;
region.span.longitudeDelta /= 150.0;
region.span.latitudeDelta /= 150.0;
PointAnnotation *point = [[PointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = userNames;
point.subtitle = userBio;
point.index = index; // Store index here.
[self.friendsMapView addAnnotation:point];
}
}
];
index = index + 1;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", [error localizedDescription]);
}];
}
-(MKAnnotationView*)mapView:(MKMapView*)friendsMapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[meetupAnn class]]) {
static NSString *identifier = @"currentLocation";
SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(pulsingView == nil) {
pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pulsingView.annotationColor = [UIColor colorWithRed:0 green:0.678431 blue:0 alpha:1];
pulsingView.canShowCallout = YES;
}
return pulsingView;
}
MKAnnotationView *view = nil;
if (annotation != self.friendsMapView.userLocation) {
NSLog(@"Friend Map Showing");
view = [self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
if (!view) {
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"mapann3.png"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
return annotationView;
}
}
return view;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if (mapView == self.friendsMapView) {
UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTappedTwo:)];
[view addGestureRecognizer:tapGesture2];
}
}
-(void)calloutTappedTwo:(UITapGestureRecognizer *) sender
{
NSLog(@"CALL OUT TWO TAPPED");
MKAnnotationView *view = (MKAnnotationView*)sender.view;
id <MKAnnotation> annotation = [view annotation];
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
PointAnnotation *selectedPoint = (PointAnnotation *) view.annotation;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
OtherUserViewController *yourViewController = (OtherUserViewController *)[storyboard instantiateViewControllerWithIdentifier:@"OtherUserViewController"];
NSMutableDictionary *dictionary = self.friendData[selectedPoint.index];
yourViewController.frienduserData = dictionary;
[self.navigationController pushViewController:yourViewController animated:YES];
}
}
Try viewForAnnotation
method with following configuration.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"annotation"];
pinView.calloutOffset = CGPointMake(0, 0);
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"annotation"]];
pinView.leftCalloutAccessoryView = iconView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}