0

に複数の注釈がありMapView、3 つの異なる配列にリストされています。メソッドを使用して- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation、注釈の Callout を変更しました。

奇妙なことは、私の UserLocation がカスタマイズされた注釈に変わっていることです。それはなぜですか? また、何が問題なのですか?

注釈のリスト方法:

myAnn = [[Annotations alloc]init];
location.latitude = 52.338847;
location.longitude = 4.937482;
myAnn.coordinate = location;
myAnn.title = @"Afvalpunt";
myAnn.subtitle = @"Rozenburglaan 1";
[category3 addObject:myAnn];

[self.locationArrays addObject:category3];

self.currentAnnotation = 0;

[self.myMapView addAnnotations:[self.locationArrays objectAtIndex:0]];

メソッドの設定方法:

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

    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapAn"];
    if (!annotationView) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapAn"];
        annotationView.canShowCallout = YES;

        //Blauw Navigatie Auto...
        UIImageView *carView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Driving"]];
        UIButton *blueView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44+30)];
        blueView.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
        carView.frame = CGRectMake(11, 14, carView.image.size.width, carView.image.size.height);
        [blueView addTarget:self action:@selector(carClicked) forControlEvents:UIControlEventTouchUpInside];
        [blueView addSubview:carView];
        annotationView.leftCalloutAccessoryView = blueView;
    } 
    return annotationView;
}
4

2 に答える 2

1

このコードを試してください。

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

    if (annotation == mapView.userLocation) return nil;
    ...

注釈userLocationの場合はnilを返し、 mapViewに青い点と円のアニメーションを表示させます。userLocationのカスタム アノテーションを表示するには、その行を削除してカスタマイズを行います。return nil;

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

static NSString* AnnotationIdentifier = @"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

if (!pinView) {

    MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
    if (annotation == mapView.userLocation)
    {
        customPinView.image = [UIImage imageNamed:@"myCarImage.png"];
    }
    else
    {
        customPinView.image = [UIImage imageNamed:@"mySomeOtherImage.png"];
        customPinView.animatesDrop = NO;
        customPinView.canShowCallout = YES;
        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }

    return pinView;
}

このコードがお役に立てば幸いです。

于 2014-04-24T10:23:40.663 に答える