2015-02-24 124 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