2011-02-10 10 views
1

以下のコードを使用して、タイトルとサブタイトルを表示でき、その中に画像を表示したいと思います。それは可能ですか?もしそうなら、私を助けてください。地図の注釈内に画像を表示

MKPointAnnotation *aAnnotationPoint = [[MKPointAnnotation alloc] init]; 

aAnnotationPoint.title = @"Virginia"; 

aAnnotationPoint.subtitle = @"Test of sub title"; 

// Add the annotationPoint to the map 
[myMapView addAnnotation:aAnnotationPoint]; 
+0

質問は "iphone-SDK-3.0" タグを持っていますが、MKPointAnnotationは4.0で追加された答えを願っています。あなたは本当に3.0を意味しますか? – Anna

+0

申し訳ありませんでしたが、タイプは – user198725878

答えて

17

私はあなたが注釈のためのコールアウトにタイトルとサブタイトル(あなたがピンを使用している場合は、ピンをタップしたときに表示される灰色のボックス)を示していると仮定しています。その場合、コールアウトにはカスタマイズ可能な2つのビューがあります(leftCalloutAccessoryViewとrightCalloutAccessoryViewは両方ともアノテーションビューで設定されています)。

SOをタップしたときにピンの上の灰色のボックスに画像を表示する場合は、あなたが望むすべてが(ない吹き出し内)地図上に直接画像をたくさん示すことであった場合、

-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    // If you are showing the users location on the map you don't want to change it 
    MKAnnotationView *view = nil; 
    if (annotation != mapView.userLocation) { 
    // This is not the users location indicator (the blue dot) 
    view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"]; 
    if (!view) { 
     // Could not reuse a view ... 

     // Creating a new annotation view, in this case it still looks like a pin 
     view = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease]; 
     view.canShowCallOut = YES; // So that the callout can appear 

     UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someName"]]; 
     myImageView.frame = CGRectMake(0,0,31,31); // Change the size of the image to fit the callout 

     // Change this to rightCallout... to move the image to the right side 
     view.leftCalloutAccessoryView = myImageView; 
     [myImageView release], myImageView = nil; 
    } 
    } 
    return view; 
} 

ただし:あなたはこのようなデリゲートメソッドを実装することで、注釈ビューをカスタマイズすることによって行うことができますピン同じデリゲートメソッドを使用して、注釈ビューの「イメージ」プロパティを次のように設定できます。

-(MKAnnotationView*)mapView:(MKMapView)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
     // If you are showing the users location on the map you don't want to change it 
     MKAnnotationView *view = nil; 
     if (annotation != mapView.userLocation) { 
     // This is not the users location indicator (the blue dot) 
     view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"]; 
     if (!view) { 
      // Could not reuse a view ... 

      // Creating a new annotation view 
      view = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease]; 

      // This will rescale the annotation view to fit the image 
      view.image = [UIImage imageNamed:@"someName"]; 
     } 
     } 
    return view; 
} 

私は、あなたの質問

+0

です。 –

+0

@ Calleth'Zion '私が知っている限り、私はその1つを引用しないでください;) –

+0

あなたの答えは+1ですが、ちょうどleftcalloutaccessryのサイズを確認したい:) –

関連する問題