Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting canShowCallOut = NO for current location annotation, iPhone

I am using custom call out (title and subtitle)for Current location icon. I tried following to disable default annotation but it does not work.

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    NSLog(@"viewForAnnotation");
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        MKAnnotationView *userLocationView = [mapView viewForAnnotation:annotation];
        userLocationView.canShowCallout = NO;
        NSLog(@"[annotation isKindOfClass:[MKUserLocation class]");
        return nil;
    }

}

Only way it works is

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)ann
{
    if([ann.annotation isKindOfClass:[MKUserLocation class]] )
    {
       [mymap deselectAnnotation:ann.annotation animated:NO];
    }
}

But it lags sometimes. Is there other way do disable default callout view for current location annotation? Any help will be appreciated.

like image 298
chatur Avatar asked Dec 09 '11 08:12

chatur


4 Answers

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;
        }
    }
like image 108
chatur Avatar answered Oct 15 '22 01:10

chatur


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

like image 6
BananaAcid Avatar answered Oct 15 '22 01:10

BananaAcid


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
    }
}
like image 2
Marius Kohmann Avatar answered Oct 15 '22 00:10

Marius Kohmann


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.

like image 1
CodeBender Avatar answered Oct 15 '22 00:10

CodeBender