2013-08-27 82 views

回答

16

可以使用自定義視圖每個註釋與一個UIView,一個UIImageView和一個標籤來顯示不同的值。這將在方法- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id)newAnnotation內完成。你必須採取UIView尺寸的雙倍透明顏色相對於UIImageView更準確地查看放大和縮小mapview。

代碼-----

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString * const kPinAnnotationIdentifier = @"PinIdentifier"; 
    if(annotation == self._mapView.userLocation) 
    { 
     return nil; 
    } 

    MyAnnotation *myAnnotation = (MyAnnotation *)annotation; 
    MKAnnotationView *newAnnotation = (MKAnnotationView*)[self._mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier]; 

    if(!newAnnotation){ 
     newAnnotation = [[MKAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:@"userloc"]; 
    } 

    NSDictionary *dict=[alertInfoArray objectAtIndex:myAnnotation.ann_tag]; 
    UIView *anView=[[UIView alloc] init]; 
    anView.backgroundColor=[UIColor clearColor]; 

    UIImageView *bgImg=[[UIImageView alloc] init]; 
    bgImg.image=[UIImage imageNamed:@""]; 
    bgImg.backgroundColor=[UIColor clearColor]; 

    UIImageView *imgView=[[UIImageView alloc] init]; 
    imgView.tag=myAnnotation.ann_tag; 

    UILabel *lblName=[[UILabel alloc] init]; 
    lblName.font=[UIFont systemFontOfSize:12]; 
    lblName.textAlignment=UITextAlignmentCenter; 
    lblName.textColor=[UIColor whiteColor]; 
    lblName.backgroundColor=[UIColor clearColor]; 
    lblName.text=TEXT YOU WANT ; 

    newAnnotation.frame=CGRectMake(0, 0, 70, 212); 
    anView.frame=CGRectMake(0, 0, 70, 212); 
    bgImg.frame=CGRectMake(0, 0, 70, 106); 
    bgImg.image=[UIImage imageNamed:@"thumb-needhelp.png"]; 

    imgView.frame=CGRectMake(8,25,55,48); 
    imgView.image=[UIImage imageNamed:@"girl-default.png"]; 
    lblName.frame=CGRectMake(5,79,60,10); 



    [anView addSubview:bgImg]; 
    [bgImg release]; 
    [anView addSubview:imgView]; 
    [imgView release]; 
    [newAnnotation addSubview:anView]; 
    [anView release]; 

    newAnnotation.canShowCallout=YES; 
    [newAnnotation setEnabled:YES]; 

    return newAnnotation; 
} 

希望這有助於。

+1

你好你有任何演示請發送給我..我在這麼麻煩.. – 2014

+1

謝謝。我解決了我的問題很容易從您的代碼謝謝兄弟.. – 2014

+2

什麼是存儲在alertInfoArray中的數據的格式和我們可以給標記註釋的功能 – Rinku

6
  1. 創建一個自定義的註釋類(MKAnnotation
  2. 創建自定義AnnotationView類(MKAnnotationView

確保您在定義標題,背景圖像和自定義註釋的其他元素AnnotationView類作爲屬性。

  1. 重寫以下方法,並設置您在AnnotationView類中定義的屬性的值。

    - (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ 
    
    if ([<Object of CustomAnnotation class> isKindOfClass:[MKUserLocation class]]) 
        return nil; 
    
    CustomAnnotation* custom_anno = (CustomAnnotation*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"custom_annotation"]; 
    
    if (!custom_anno){ 
    custom_anno = [[CustomAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:@"custom_annotation"]; 
    custom_anno.frame = CGRectMake(0, 0, 250, 57.5); 
    
    custom_anno.canShowCallout = NO;//CHange if you want to change callout behaviour (thats is it's abilty to apper). I set it top no because i did not want a callout. 
    
    UIImageView* icon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)]; 
    [icon setImageWithURL:<your image> placeholderImage:<your placeholder image>];//placeholder image = nil if you do not want one. 
    icon.backgroundColor = [UIColor lightGrayColor]; 
    
    UILabel* name = [[UILabel alloc] initWithFrame:CGRectMake(50, 5, 200, 20)]; 
    name.text = nameValue; 
    
    UILabel* category = [[UILabel alloc] initWithFrame:CGRectMake(50, 25, 200, 20)]; 
    category.text = otherValue; 
    
    [custom_anno addSubview:icon]; 
    [custom_anno addSubview:name]; 
    [custom_anno addSubview:category]; 
    } 
    return custom_anno; 
    } 
    

    我的自定義註釋類是'Annotation',自定義註釋視圖類是'CustomAnnotation'。根據您的需求改變。

之後,只需創建一個Annotation類的對象,並使用它。

編輯:

確保您覆蓋follwing方法詮釋他的自定義註解視圖類:

在.H

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier; 

中的m:

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{ 
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; 
if (self) { 
} 
return self; 

}

編輯2:

你可以用它在你的視圖控制器是這樣的:

MKMapView* cellMap = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)]; 
cellMap.delegate = self; 
cellMap.userInteractionEnabled = NO; 

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(<your coordinates>); 
MKCoordinateSpan span = MKCoordinateSpanMake(0.04, 0.04); 
MKCoordinateRegion region = MKCoordinateRegionMake(center, span); 
cellMap.showsUserLocation = NO; 
[cellMap setRegion:region animated:YES]; 

Annotation* anon = [[Annotation alloc] init]; 
anon.coordinate = center; 

[cellMap addAnnotation:anon]; 
[checkIn addSubview:cellMap]; 
+1

有你的任何演示? – 2014

+1

非常感謝..我解決了我的問題... – 2014

相關問題