简体   繁体   中英

Custom Annotation Callout View or Custom View ?

I'm currently building an "Uber-like" application, not in the same business but same design, and I would like to know what should I do if I want to represent their "Set pickup location" View/Button.

I already saw this post : Customize MKAnnotation Callout View? which was about Custom Annotation Callout View

In older version it looked like a Custom Callout View, but now it's a bit different, and I don't know where to start if I want to have the same result :

在此处输入图片说明

Thanks for your help !

To set different image on left accessory and right accessory, use this code.

 // set different pins colors
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *annotationView = nil;
if( [annotation isKindOfClass:[YOURANNOTATION class] ] )
{
    static NSString * AnnotationID = @"YOURANNOTATION";
    annotationView = [self.mapView  dequeueReusableAnnotationViewWithIdentifier:AnnotationID];
    if( annotationView == nil )
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                       reuseIdentifier:AnnotationID] ;

    }
    UIImage * flagImage = nil;

            flagImage = [UIImage imageNamed:@"marker-map@1x.png"];
            [annotationView setImage:flagImage];
            annotationView.canShowCallout = YES;

            //   add an image to the callout window
            UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"marker-map@1x.png"]];
            annotationView.leftCalloutAccessoryView = leftIconView;


    //adding right button accessory
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    annotationView.rightCalloutAccessoryView = infoButton;

    //image size and adding image on left accessory
    CGRect resizeRect;
    resizeRect.size = flagImage.size;
    resizeRect.origin = (CGPoint){0.0f, 0.0f};
    UIGraphicsBeginImageContext(resizeRect.size);
    [flagImage drawInRect:resizeRect];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
    annotationView.image = resizedImage;

}
return annotationView;

}

Then to call selected annotation

  - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
 {


YOURANNOTATION *annotation=(YOURANNOTATION*)view.annotation;

 //do something with your annotation

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM