Question

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;
}
Was it helpful?

Solution

Try this code.

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

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

We return nil if the annotation is userLocation to let the mapView display the blue dot & circle animation. In order to show our custom annotation for userLocation just remove the line return nil; and do your customization there.

- (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;
}

Hope this code is useful for you.

OTHER TIPS

Try this code

- (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
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top