自定义注释标注视图或自定义视图?

我目前正在构建一个“类似优步”的应用程序,不是在相同的业务中,而是相同的设计,我想知道如果我想代表他们的“设置取件位置”视图/按钮我该怎么办 .

我已经看过这篇文章:Customize MKAnnotation Callout View?这是关于自定义注释标注视图

在旧版本中它看起来像一个自定义标注视图,但现在它有点不同,如果我想得到相同的结果我不知道从哪里开始:

enter image description here

谢谢你的帮助 !

回答(1)

2 years ago

要在左侧附件和右侧附件上设置不同的图像,请使用此代码 .

// 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;

}

然后调用选定的注释

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


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

 //do something with your annotation

 }