0

カスタム注釈をマップに表示し、現在の場所をマップ ビューの標準ピンとして青色で表示したいと考えています。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *identifier = @"MapPin";
    if ([annotation isKindOfClass:[MyAnnotations class]]) {
        MyAnnotations *ann= annotation;
        MKAnnotationView *annotationView = (MKAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            if (ann.custom){
                annotationView.image = [UIImage imageNamed:@"custom.png"];
            }else{
               //?? annotationView.image = [UIImage imageNamed:@"bluePin.png?"];
            }
        } else {
            annotationView.annotation = annotation;
        }
        if(ann.custom){
            UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [nextButton addTarget:self action:@selector(annotationPicked) forControlEvents:UIControlEventTouchUpInside];
            annotationView.rightCalloutAccessoryView=nextButton;
        }
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.multipleTouchEnabled = NO;
        return annotationView;
    }
    return nil;
}
4

2 に答える 2

0

customプロパティが注釈とマップ ビューのユーザーの位置を区別する場合、そのケースは注釈のクラスをチェックする最初のものによって既に処理されておりifcustomプロパティは不要です。

マップ ビューのユーザー位置の注釈はタイプでMKUserLocationあるため、コードはその場合に返され、マップ ビューには標準の青い点が表示されます (が であるとnil仮定)。showsUserLocationYES


ただし、プロパティが独自のcustom注釈の 2 つのタイプを区別することである場合、1 つの問題は、再利用された注釈ビューを適切に処理しないことです ( の場合)。annotationView != nil

注釈ビューを再利用する場合、そのimagerightCalloutAccessoryViewが現在のものに適していない可能性があるannotationため、ビューを再利用するかどうかにかかわらず、これらのプロパティを設定 (またはクリア) する必要があります。例えば:

if ([annotation isKindOfClass:[MyAnnotations class]]) {
    MyAnnotations *ann= annotation;
    MKAnnotationView *annotationView = (MKAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.multipleTouchEnabled = NO;
    } else {
        annotationView.annotation = annotation;
    }

    //set view properties that depend on annotation-specific properties
    //regardless of whether view is new or re-used...
    if (ann.custom)
    {
        annotationView.image = [UIImage imageNamed:@"custom.png"];

        UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [nextButton addTarget:self action:@selector(annotationPicked) forControlEvents:UIControlEventTouchUpInside];
        annotationView.rightCalloutAccessoryView=nextButton;
    }
    else
    {
        annotationView.image = [UIImage imageNamed:@"bluePin.png"];
        annotationView.rightCalloutAccessoryView = nil;
    }

    return annotationView;
}

ただし、customプロパティがマップ ビューのユーザーの場所からすべてclassの注釈を分離するだけの場合は、最初は不要です (注釈を確認してに設定showsUserLocationする限りYES)。

于 2013-08-11T12:41:55.610 に答える