3

(理論上) MKPointAnnotation に関連付けられた MKPinAnnotationView がマップに表示されない理由がわかりません。実際、ピンは表示されますが、本来の紫色ではありません...

コードは次のとおりです。

MKPointAnnotation *myPersonalAnnotation= [[MKPointAnnotation alloc]init];
myPersonalAnnotation.title= [appDelegate.theDictionary objectForKey:@"theKey"];
myPersonalAnnotation.coordinate=CLLocationCoordinate2DMake(6.14, 10.7);
MKPinAnnotationView *myPersonalView=[[MKPinAnnotationView alloc] initWithAnnotation:myPersonalAnnotation reuseIdentifier:@"hello"];
myPersonalView.pinColor=MKPinAnnotationColorPurple;
[myMap addAnnotation:myPersonalAnnotation];
4

1 に答える 1

2

デフォルトの赤いピンとは異なる注釈ビューを作成する場合は、それを作成して、マップビューのviewForAnnotationデリゲートメソッドで返す必要があります。

viewForAnnotationマップは、アノテーション(組み込みのユーザーの場所または追加したアノテーション)を表示する必要があるときはいつでも、デリゲートメソッドを自動的に呼び出します。

myPersonalViewを呼び出す前にのローカル作成を削除し、代わりにメソッド addAnnotationを実装します。viewForAnnotation

例えば:

//in your current method...
MKPointAnnotation *myPersonalAnnotation= [[MKPointAnnotation alloc]init];
myPersonalAnnotation.title= [appDelegate.theDictionary objectForKey:@"theKey"];
myPersonalAnnotation.coordinate=CLLocationCoordinate2DMake(6.14, 10.7);
[myMap addAnnotation:myPersonalAnnotation];

//...

//add the viewForAnnotation delegate method...
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //if annotation is the user location, return nil to get default blue-dot...
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //create purple pin view for all other annotations...
    static NSString *reuseId = @"hello";

    MKPinAnnotationView *myPersonalView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (myPersonalView == nil)
    {
        myPersonalView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        myPersonalView.pinColor = MKPinAnnotationColorPurple;
        myPersonalView.canShowCallout = YES;
    }
    else
    {
        //if re-using view from another annotation, point view to current annotation...
        myPersonalView.annotation = annotation;
    }

    return myPersonalView;
}


マップビューのdelegateプロパティが設定されていることを確認してください。設定されていない場合、デリゲートメソッドは呼び出されません。
コードでは、を使用するかmyMap.delegate = self;(たとえば、 )を使用するか、がの場合はviewDidLoadInterfaceBuilderで接続します。myMapIBOutlet

于 2012-11-19T23:01:27.480 に答える