5

私の MKPointAnnotation は、このコードでカスタムにする必要があります:

-(MKPointAnnotation*)setAnnotation: (NSString*) title atLocation:(CLLocationCoordinate2D)Location withImage:(UIImage*) LocationImage{
    Pin = [[MKPointAnnotation alloc] init];
    Pin.title = title;
    [Pin setCoordinate:Location];
    [self mapView:mapView viewForAnnotation:Pin].annotation = Pin;
    return Pin;
}



-(MKAnnotationView *)mapView: (MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>) annotation{

        MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];


        if(!pinView){
            pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            pinView.enabled = YES;
            UIButton *PicButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [PicButton addTarget:self action:@selector(showLocationPicture) forControlEvents:UIControlEventTouchUpInside];\
            pinView.rightCalloutAccessoryView = PicButton;
        }
        else{
            pinView.annotation = annotation;
        }
    return pinView;
}

それでも、何らかの理由でピンがまだデフォルトのままです。誰か助けてくれませんか? ありがとう

4

1 に答える 1

10

自称してはいけませんviewForAnnotation。注釈はマップ ビューにのみ追加する必要があります。注釈は、特定の瞬間に表示されるものを決定し、ユーザーを呼び出しviewForAnnotationます。したがって:

  • 異なる注釈が異なる画像を持つことができることを考えると、サブクラスMKPointAnnotation化してプロパティを与える必要がありますimageName(それ自体ではなく、それ自体を作成するために使用できるUIImage名前または識別子に注意してください)。viewForAnnotationUIImage

    @interface MyAnnotation : MKPointAnnotation
    @property(nonatomic, strong) NSString *imageName;
    @end
    
    @implementation MyAnnotation
    @end
    
  • 注釈 (注釈ビューではない) をマップビューに追加します。例:

    - (id<annotation>)addAnnotationWithTitle:(NSString *)title coordinate:(CLLocationCoordinate2D)coordinate imageName:(NSString *)imageName
    {
        MyAnnotation *annotation = [[MyAnnotation alloc] init];
        annotation.title = title;
        annotation.coordinate = coordinate;
        annotation.imageName = imageName;
        [mapView addAnnotation:annotation];
        return annotation;
    }
    

    Pinクラス ivar/propertyを作成することはお勧めしません。変数名には camelCase を使用し (小文字で開始)、メソッド名を からなどに変更しsetAnnotationます。addAnnotationWithTitle

  • コントローラーが mapView のデリゲートとして指定されていることを確認してください。

  • viewForAnnotationが呼び出されます (注釈が表示されている場合) 。おそらく次のようになります。

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if ([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
    
        if ([annotation isKindOfClass:[MyAnnotation class]])
        {
            MyAnnotation *customAnnotation = annotation;
    
            static NSString *annotationIdentifier = @"MyAnnotation";
            MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
            if (!annotationView)
            {
                annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
                annotationView.canShowCallout = YES;
                annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            }
            else
            {
                annotationView.annotation = annotation;
            }
    
            annotationView.image = [UIImage imageNamed:customAnnotation.imageName];
    
            return annotationView;
        }
    
        return nil;
    }
    

    これは、カスタム注釈ビューでは実行できないオプションであることにanimatesDrop注意してください。MKPinAnnotationViewただし、必要に応じて簡単に実装できます ( MKAnnotationView を使用してカスタムの「ピンドロップ」アニメーションを作成するにはどうすればよいですか? を参照してください)。最初に基本的な注釈ビューに取り組み、後でカスタム注釈ビューのドロップ アニメーションを確認することをお勧めします。

于 2013-05-12T12:09:36.863 に答える