0

I am trying to use some delegate methods in MKMapViewDelegate. Specifically, I want to give the pop out an accessory arrow so that when user touches it, it launches native map application (for full blown mapping functions.)

I think I understand correctly that once the VC is set up as a delegate of itself then the protocol methods get called automatically. In that case do delegate methods get called automatically or do you have to do something else?

Here is method to launch map.

-(void) geoCodeAndMapIt {
    NSString* location = @"156 University Ave, Palo Alto, CA 94301";
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder 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 = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);//5000 is meters
                     region.span.longitudeDelta /= 8.0;
                     region.span.latitudeDelta /= 8.0;

                     [self.mapView setRegion:region animated:YES];
                     [self.mapView addAnnotation:placemark];
}

Here is method that should add accessory arrow to callout but is not firing:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
    annotationView.canShowCallout = YES;
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return annotationView;
}

How do I get this method to fire so I get accessory arrow? Thanks for any suggestions.

user1904273
  • 4,562
  • 11
  • 45
  • 96
  • 1
    "once the VC is set up as a delegate of itself": Not quite accurate. Actually, the VC is set up as a delegate of the MKMapView which (I assume) is contained in the VC. You do this either in code using `mapView.delegate = self;` or connecting the map's delegate outlet to the VC in the storyboard. –  May 11 '15 at 12:00
  • That was why I was failing to get the accessory view. When I included your line in viewDidLoad, I get the accessory. However, I don't get the arrow...instead I get the little information button. Did this change in IOS7 or how can I get the arrow? – user1904273 May 11 '15 at 17:00
  • 1
    Regarding arrow for UIButtonTypeDetailDisclosure, see http://stackoverflow.com/questions/22012468/mkannotationview-always-shows-infobutton-instead-of-detaildisclosure-btn. You'll have to use a custom button type with your own arrow image. –  May 11 '15 at 17:10

1 Answers1

3

Why don't you use this method when you click the button in you annotation view this method is fired and you can perform your custom action there.

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
        //Do whatever you want to do here.

        PropertyLocation *location = (PropertyLocation*)view.annotation;

        for(PropertyDTO *property in self.items)
        {
            if(property.location == location)
            {
                PropertyDetailVC *detailVC = [PropertyDetailVC propertyDetailVCWithNodeID:property.nodeID];

                [self.navigationController pushViewController:detailVC animated:YES];
            }
        }

    }
Adeel Ahmed
  • 161
  • 1
  • 8