MKUserLocation is selectable, falsely intercepting

2019-04-28 04:44发布

I have a normal map in my iOS app where "Shows Users Location" is enabled - meaning I have my normal blue dot on the map, showing my position and accuracy info. The callout is disabled in code.

But I also have custom MKAnnotationViews that are plotted around the map which all have custom callouts.

This is working fine, but the problem is that when my location is on the location of a MKAnnotationView, the blue dot (MKUserLocation) intercepts the touch, so the MKAnnotationView doesn't get touched.

How can I disable the user interaction on the blue dot so that the touches are intercepted by the MKAnnotationViews and not the blue dot?

This is what I do so far:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation;
{
    if (annotation == self.mapView.userLocation)
    {
        [self.mapView viewForAnnotation:annotation].canShowCallout = NO;
        return [self.mapView viewForAnnotation:annotation];
    } else {
        ...
    }
}

2条回答
三岁会撩人
2楼-- · 2019-04-28 04:52

Disabling the callout doesn't disable touches on the view (didSelectAnnotationView will still get called).

To disable user interaction on an annotation view, set its enabled property to NO.

However, instead of setting enabled to NO in the viewForAnnotation delegate method, I suggest doing it in the didAddAnnotationViews delegate method instead and in viewForAnnotation, just return nil for MKUserLocation.

Example:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation;
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }

    //create annotation view for your annotation here...
}

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *av = [mapView viewForAnnotation:mapView.userLocation];
    av.enabled = NO;  //disable touch on user location
}
查看更多
何必那么认真
3楼-- · 2019-04-28 05:06

Swift 4.2 Example:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)   ->     MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
// Add custom annotation views here.
}

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView])      {
    // Grab the user location annotation from your IB Outlet map view.
    let userLocation = mapView.view(for: mapView.userLocation)
    userLocation?.isEnabled = false
}
查看更多
登录 后发表回答