2012-05-01 21 views
9

私はobjective-cでmapkitを初めて使っています。私はmapviewでカスタム注釈を追加することができます。mapviewでカスタムコールアウトビューを追加する方法

私は画像

like this以下のようなカスタム吹き出しビューを配置する必要があります。

しかし、私はどのように私はこのような吹き出しビューを設計することができませんでした。

私は注釈メソッドのビューで吹き出しを追加する必要があることを知っています。

- (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はここ.. – Nit

+0

あなたのリプレイニットのおかげで、あなたが投稿したリンクが動作していない、それは結果が見つかりませんでした –

+0

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

答えて

0

基本的に何がやりたいことはあなたのMKMapViewDelegateインスタンスに次のメソッドを実装することで、新しいカスタムビューを追加します:

- (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

あなたはコールアウトを自分で見実装、および

を使用する必要があります。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 

この代理人は、コールアウトビューを適切な場所に表示するように、

IOSカスタムコールアウトビューに任意の方法を持っていない、まだ

+1

これは@ pnolletの回答とどのように違いますか – Mark

0

これに対する一つの解決策は、正確な親のUIViewに注釈選択された相対の座標、次いでMKMapViewの上に直接UIViewの描画検出することであろう。

ここで役立つかもしれないスニペットです:

- (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 
} 
関連する問題