0
votes

I have created a custom MKPinAnnotationView for all my map pins, but I'm having a problem customising the user location pin. If I create a custom pin for it, then I lose the blue pulsating dot and it is replaced with a pin, which I don't want. So I return nil in the viewForAnnotation method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{   
    if(annotation == self.mapView.userLocation)
    {
        // show the blue dot for user's GPS location
        return nil;
    }

    ... code for custom annotation pins goes here
}

However, I do want it to invoke a custom callout view when I tap on it. I've tried adding a subview when the blue dot has been selected:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{    
    if(view == [self.mapView viewForAnnotation:self.mapView.userLocation])
    {
        MKAnnotationView *userLocationView = [self.mapView viewForAnnotation:self.mapView.userLocation];

        userLocationCalloutView = [[MyAnnotationCalloutView alloc] initWithFrame:CGRectMake(-113, -58, 247, 59)];
        [userLocationCalloutView.rightAccessoryButton addTarget:self action:@selector(pinButtonTapped) forControlEvents:UIControlEventTouchDown];
        [userLocationView addSubview:userLocationCalloutView];
    }
}

But the problem with this is that the callout accessory button is outside of the hit area, and I cannot add a hitTest:withEvent because the blue dot annotation view is private... Any ideas?

1

1 Answers

2
votes

You can get the built MKAnnotationView for the userLocation annotation in the delegate method mapView:didAddAnnotationViews:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    for(MKAnnotationView *view in views)
    {
        if (view.annotation == mapView.userLocation)
        {
            view.canShowCallout = YES;
            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            break;
        }
    }
}