setting canShowCallOut = NO for current location annotation, iPhone

一世执手 提交于 2019-12-04 23:20:14

To get this done one need to get reference of current location MKAnnotationView. One can get this reference anywhere but it is better to get it as soon as user location is determined.

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{
 MKAnnotationView* annotationView = [mapView viewForAnnotation:userLocation];
annotationView.canShowCallout = NO;

}

Or use following method

 - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
MKAnnotationView *aV; 
     for (aV in views) {
            if ([aV.annotation isKindOfClass:[MKUserLocation class]]) {
                MKAnnotationView* annotationView = aV;
                 annotationView.canShowCallout = NO;

            }
    }

and if want to change in canShowCallout property in runtime then one can use following

for (AnnotationClass* annotation in mapView.annotations) 
    {
        if([annotation isKindOfClass:[MKUserLocation class]] )
        {
             MKAnnotationView* annotationView = [mapView viewForAnnotation:annotation];
             annotationView.canShowCallout = NO;
        }
    }

an update to this, using swift (based on chatur's answer):

func mapView(mapView: MKMapView!, didAddAnnotationViews views: [MKAnnotationView]!) {

    for view in views {
        if view.annotation.isKindOfClass(MKUserLocation) {
            view.canShowCallout = false
        }
    }

}

Note: using this, i did not require anything else to make it work

Swift:

This worked for me: Just add this delegate method.

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    for var view in views {
        view.canShowCallout = false
    }
}

This worked for me on Swift 3.0 / iOS 10.

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    (views.filter { $0.isKind(of: MKUserLocation.self) }).first?.canShowCallout = false
}

Rather than loop through all the views, I just rely on the filter command, followed by the optional call on first since there should only be one location, and then set the value to false.

// Objective-C

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
        MKAnnotationView *userLocationView = [mapView viewForAnnotation:userLocation];   
        userLocationView.canShowCallout = NO;
}  

// Swift 4.2

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        let userAnnotationView = mapView.view(for: userLocation)
        userAnnotationView?.canShowCallout = false
}

EDIT

Sorry for getting you wrong.

I banged my head on this.

This is the only way i have succeeded doing it. the problem with this method is that you have to change the UserLocation view. So it might be not so user friendly.

Any way:

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

        MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation                                                                           reuseIdentifier:nil];
        annotationView.image = [UIImage imageNamed:@"your-icon.png"];
        annotationView.enabled=NO;
         return annotationView;
    };
     ........}

Have to run to the kindergarden to take my kid :)

Good luck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!