Showing a custom UIView in an output leader (annotation) on a map using MapKit

In my application, I want to display a user view (with buttons) if the user touches the annotation on the map (standard output). I know that I can subclass MKAnnotationView and change the appearance of the pin. but how can I change the appearance of the additional informational representation that appears when the user touches a contact (by default, it displays the name and subtitles of the MKAnnotation object). How can i do this?

Thank.

+3
source share
2 answers
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation
{
    MKPinAnnotationView *pinView = nil;
    static NSString *defaultPinID = @"ReusedPin";
    pinView = (MKPinAnnotationView*)[mVdequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    if (((PinAnnotationView*)annotation).tag == 0 )
    {
        pinView.pinColor = MKPinAnnotationColorPurple;
    }
    else {
        pinView.pinColor = MKPinAnnotationColorRed;
    }
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    UIImageView *pinImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-5, 0, 34, 34)];
    UIImage *pinImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"]];
    pinImageView.image = pinImage;
    [pinImage release]; 
    [pinView addSubview:pinImageView];
    [pinImageView release];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btn.tag = ((PinAnnotationView*)annotation).tag;
    pinView.rightCalloutAccessoryView = btn;
    return pinView;
}

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    if ( control.tag !=0) {
        ShowProviderDetailVC  *viewControlle = [[ShowProviderDetailVC alloc]initWithNibName:@"ShowProviderDetailVC" bundle:nil];
        viewControlle.lastViewName = @"SearchView";
        for (NSMutableDictionary* dict in globalLocArray) {
            if ( control.tag ==[[dict valueForKey:@"ID"] intValue] )
            {
                viewControlle.providerInfoDict = dict;
            }
     }
    [self.navigationController pushViewController:viewControlle animated:YES];
    [viewControlle release];
}
+4
source

Source: https://habr.com/ru/post/1785892/


All Articles