2015-02-24 84 views
1

首先,我使用主引脚来显示批注数组。一切都按预期工作。iOS MKMapView的自定义注释

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

  if (annotationView == nil) { 
      annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"cluster"]; 
      annotationView.canShowCallout = YES; 
      annotationView.image = [UIImage imageNamed:@"map_cluster"]; 
     } 

有了这个,我只能用图像替换默认的引脚。但这不是我需要的。

http://imgur.com/nhUIvdx

我来自一个Android背景其中I通过膨胀的布局为群集(又名注释在IOS)解决了这个问题。在这个XML布局中,我定位了一个容器(白色框架),一张图片和一个计数器。 结果可以看到蜜蜂在下面的图片:

http://imgur.com/QoAQQES

我怎样才能做到这一点的iOS? 在此先感谢您的任何建议!

回答

0

相当迟了回应,但我想其他人仍然会有兴趣的

在MKAnnotationView子类:

首先,定义一些尺寸:

如果您希望背景是一个图像,而不仅仅是一个颜色

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帮手,没有什么特别的:

+ (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; 
} 

我知道有相当多的事情,以改善,但到那时,我希望这会帮助别人。 :)

2

在这里你可以找到如何创建一个自定义的注释: custom annotation for maps

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

+0

感谢Adela的建议,绝对有帮助! – 2015-06-15 20:54:30

+0

欢迎:) @ emn.mun – Adela 2015-06-16 15:18:30