0

I have multiple Annotation in a MapView, listed in 3 different arrays. I've used the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation method to change te Callout of the annotation.

The weird thing is that my UserLocation is changing into an Customised Annotation. Why is that, and what could be the problem?

How I listed my Annotations:

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]];

How my Method is set up:

- (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 回答
1

试试这个代码

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{
     if ([annotation isKindOfClass:[MKUserLocation class]] || annotation==mapView.userLocation)
    {
        return nil;//Here you can also customise your pin if you dont want pin then just return nil.
    }
    else
    {
     //your code
    }
}
于 2014-04-24T10:25:15.273 回答