2012-05-01 47 views
9

Tôi mới sử dụng bản đồ trong mục tiêu-c. Tôi có thể thêm chú thích tùy chỉnh trong mapview.cách thêm chế độ xem chú dẫn tùy chỉnh trong chế độ xem bản đồ

tôi cần phải đặt giao diện tùy chỉnh chú thích như dưới đây hình ảnh

like this.

Nhưng tôi không hiểu cách tôi có thể thiết kế chế độ xem chú thích như thế này.

tôi biết tôi cần phải thêm chú dẫn trong chế độ xem cho phương pháp chú thích.

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString *AnnotationViewID = @"annotationViewID"; 

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 

    if (annotationView == nil) 
    { 
     annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease]; 
    } 

    annotationView.image = [UIImage imageNamed:@"blue_without_pin.png"]; 
    annotationView.annotation = annotation; 


    return annotationView; 
} 
+0

http://shawnsbits.com/blog/2011/04/12/custom-map-pins-for-mapkit/..visit đây .. – Nit

+0

cảm ơn cho nit phát lại của bạn, nhưng liên kết mà bạn đăng không hoạt động, nó cho thấy không có kết quả nào được tìm thấy –

+0

http://shawnsbits.com/blog/2010/12/23/mapkit-overlays-session-1-overlay-map/ – hanumanDev

Trả lời

0

Về cơ bản những gì bạn muốn làm là thêm một cái nhìn tùy chỉnh mới bằng cách thực hiện các phương pháp sau đây trong trường hợp MKMapViewDelegate của bạn:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
    CGPoint annotationCenter = [mapView convertCoordinate:view.annotation.coordinate toPointToView:mapView]; 
    [self displayCustomCalloutForAnnotation:view.annotation center:annotationCenter]; 
} 
0
@interface CustomAnnotaionView : MKAnnotationView 




@property (strong, nonatomic) UIView *calloutView; 




-(void)setSelected:(BOOL)selected animated:(BOOL)animated; 




@end 




@implementation CustomAnnotaionView 




@synthesize calloutView = _calloutView; 




-(void)setSelected:(BOOL)selected animated:(BOOL)animated 





{ 

    [super setSelected:selected animated:animated]; 

    if(selected) 
    { 

     [self.calloutView setFrame:CGRectMake(30, 130, 250, 135)]; 
     [self.calloutView sizeToFit]; 
     self.calloutView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"popup.png"]]; 
     [self addSubview:self.calloutView]; 

    } 
    else 
    { 

     [self.calloutView removeFromSuperview]; 
    } 

} 

-(void)didAddSubview:(UIView *)subview 
{ 

    if([[[subview class]description]isEqualToString:@"UICalloutView"]) 
    { 

     [subview removeFromSuperview]; 

    } 
} 

Use this customAnnotationView class in MapView delegate method, 

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id 

<MKAnnotation>)annotation 

{ 



    if([annotation isKindOfClass:[MapAnnotation class]]) 
    { 
     CustomAnnotationView *annotationView = (CustomAnnotaionView*)[theMapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotationView"]; 
     if(!annotationView) 
     { 
     MapAnnotation *annotation = (MapAnnotation *)annotation; 

     annotationView = [[CustomAnnotaionView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotationView"]; 

      [annotationView setCanShowCallout:YES]; 
     } 
     else 
      annotationView.annotation = annotation; 

     return annotationView; 

    } 
    return nil; 

} 
-3

Bạn phải thực hiện một callout xem chính mình, và sử dụng

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 

đại biểu này để làm cho chế độ xem chú thích hiển thị ở đúng vị trí,

IOS không có bất kỳ phương pháp để xem chú thích tùy chỉnh chưa

+1

Điều này khác với câu trả lời của @ pnollet như thế nào – Mark

0

Một giải pháp cho việc này sẽ được phát hiện chính xác tọa độ của chọn chú thích liên quan đến các UIView phụ huynh và sau đó vẽ một UIView trực tiếp trên đầu trang của MKMapView.

Dưới đây là một đoạn mã có thể giúp:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    CGPoint pointInMap = [self.mapView convertCoordinate:buildingAnnotation.coordinate 
              toPointToView:self.view]; 

    // Hide the default callout generated by MKMapView 

    [mapView deselectAnnotation:view.annotation animated:NO]; 

    // Create a custom callout view and center the arrow on the pointInMap CGPoint 
} 
Các vấn đề liên quan