Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS针对MKMapView的自定义批注_Ios_Mkmapview_Mapkit_Mkannotation_Mkannotationview - Fatal编程技术网

iOS针对MKMapView的自定义批注

iOS针对MKMapView的自定义批注,ios,mkmapview,mapkit,mkannotation,mkannotationview,Ios,Mkmapview,Mapkit,Mkannotation,Mkannotationview,首先,我使用主销来显示注释数组。一切正常 我的问题是:如何为注释创建自定义视图? 我不是指替换MKPinAnnotationView的图像 if (annotationView == nil) { annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"cluster"]; annotationV

首先,我使用主销来显示注释数组。一切正常

我的问题是:如何为注释创建自定义视图? 我不是指替换MKPinAnnotationView的图像

            if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"cluster"];
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"map_cluster"];
        }
有了这个,我只能用图像替换默认pin。但这并不是我所需要的全部

我来自Android背景,通过将布局膨胀为集群(iOS中的又称注释)解决了这个问题。在这个XML布局中,我放置了一个容器(白色框架)、一张图片和一个计数器。 结果可以在下图中看到:

我如何在iOS中做到这一点?
提前感谢您的建议

在这里,您可以找到如何创建自定义批注:


如果你有任何问题,你可以问我:)

反应很晚,但我想其他人还是会感兴趣的

在MKAnnotationView子类中:

首先,定义一些维度:

#define ckImageHeight 65
#define ckImageWidth  55
#define kBorder 5
然后定义注释视图的框架:

self.frame = CGRectMake(0, 0, ckImageWidth, ckImageHeight);
如果希望背景是图像而不仅仅是颜色:

self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"map_checkin"]];
然后为图像创建一个占位符

CGRect checkInPictureRect = CGRectMake(kBorder, kBorder, ckImageWidth - 9 , ckImageWidth - 9);
UIView *checkInPictureView = [[UIView alloc]initWithFrame:checkInPictureRect];
然后,乐趣开始了:

// Crop image
UIImage *croppedImage = [ImageHelper centerCropImage:image];

// Resize image
CGSize checkInPictureSize = CGSizeMake(checkInPictureRect.size.width*1.5, checkInPictureRect.size.height*1.5);
UIGraphicsBeginImageContext(checkInPictureSize);
[croppedImage drawInRect:CGRectMake(0, 0, checkInPictureRect.size.width*1.5, checkInPictureRect.size.height*1.5)];
UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *imageView = [[UIImageView alloc] initWithImage:resizedImage];
                            imageView.frame = checkInPictureView.bounds;
                            [checkInPictureView addSubview:imageView];
                            [self addSubview:checkInPictureView];

// Counter
UIView *counterView = [[UIView alloc]initWithFrame:CGRectMake(45, -2, 15, 15)];
counterView.opaque = YES;
(checkIn.isNow) ? [counterView setBackgroundColor:[UIColor enloopBlue]] : [counterView setBackgroundColor:[UIColor enloopGreen]];
counterView.layer.cornerRadius = 8;

self.counterLabel = [[UILabel alloc] init];
self.counterLabel.frame = CGRectMake(4, 2, 10, 10);

if (self.count >= 10) {
    counterView.frame = CGRectMake(45, -2, 18, 18);
    self.counterLabel.frame = CGRectMake(3, 3, 12, 12);
}

[self.counterLabel setTextColor:[UIColor whiteColor]];
[self.counterLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 11.0f]];
[self.counterLabel setText:[[NSString alloc] initWithFormat:@"%lu", (unsigned long)self.count]];
[counterView addSubview:self.counterLabel];
[counterView bringSubviewToFront:self.counterLabel];
[self addSubview:counterView];
对于
centerCropImage
helper,没有什么特别的:

+ (UIImage *)centerCropImage:(UIImage *)image {
    // Use smallest side length as crop square length
    CGFloat squareLength = MIN(image.size.width, image.size.height);
    // Center the crop area
    CGRect clippedRect = CGRectMake((image.size.width - squareLength) / 2, (image.size.height - squareLength) / 2, squareLength, squareLength);

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
    UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return croppedImage;
}

我知道还有很多地方需要改进,但在此之前,我希望它能帮助其他人。:)

谢谢阿德拉的建议,绝对有帮助!