ViewForAnnotation is not being called
Asked Answered
K

9

19

I am trying to make a map, where I can see my current location, and see what the street is called.

so far, I am able to put a pin on my map, but for some reason, I am not getting the callout. and I have put a NSLog in my viewForAnnotation method, but it is not being called, so i wasn't able to test it.

can someone help me?

-(void)lat:(float)lat lon:(float)lon
{
    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = lon;
    NSLog(@"Latitude: %f, Longitude: %f",location.latitude, location.longitude);
    //One location is obtained.. just zoom to that location

    MKCoordinateRegion region;
    region.center=location;
    //Set Zoom level using Span
    MKCoordinateSpan span;
    span.latitudeDelta=.005f;
    span.longitudeDelta=.005f;
    region.span=span;
    [map setRegion:region animated:TRUE];

    //MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
    //geocoder.delegate=self;
    //[geocoder start];
    if (cPlacemark != nil) {
        [map removeAnnotation:cPlacemark];
    }
    cPlacemark=[[CustomPlacemark alloc] initWithCoordinate:location];
    cPlacemark.title = mPlacemark.thoroughfare;
    cPlacemark.subtitle = mPlacemark.locality;
    [map addAnnotation:cPlacemark];
    [cPlacemark release];

    [mLocationManager stopUpdatingLocation];
}

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

// try to dequeue an existing pin view first
if ([annotation isKindOfClass:[CustomPlacemark class]]){
MKPinAnnotationView *pinView=(MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:@"customIdentifier"];

if (!pinView)
{
    // if an existing pin view was not available, create one
    MKPinAnnotationView* cPinAnnoView = [[[MKPinAnnotationView alloc]
                                           initWithAnnotation:annotation reuseIdentifier:@"customIdentifier"] autorelease];
    cPinAnnoView.pinColor = MKPinAnnotationColorPurple;
    cPinAnnoView.animatesDrop = YES;
    cPinAnnoView.canShowCallout = YES;
    // Add button
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [leftButton addTarget:self action:@selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside];
    cPinAnnoView.leftCalloutAccessoryView = leftButton;

} else
    {
        pinView.annotation = annotation;
    }
    return pinView; 
}
return nil;

}

Right now I have customized my viewForAnnotation to be like this. But I still can't get a callout from my pin and the pin remains red. But it should be purple of nothing at all

Kiloton answered 29/9, 2010 at 12:21 Comment(4)
If your map is being loaded from a nib, did you remember to wire it up? Did you remember to set the delegate?Cantle
my delegate is "wired up" to my searchbar and searchdisplaycontroller it's working now:P thanks I still need to figure out why i don't get a callout thoughKiloton
Can you post your CustomPlacemark class? I can't see any reason why this wouldn't work...Adduction
I already have found the answer I didn't set the delegate to my mapviewKiloton
E
33

I had the same problem which was not setting the MapView delegate to the File Owner.

  1. Open your nib
  2. Right click on the MapView
  3. Drag the delegate to the File's Owner
Elective answered 21/2, 2012 at 18:57 Comment(1)
or if using storyboards - CTRL click the MapView and drag to the little orange ViewController icon (on the black bar just below the window)Cymogene
P
24

I had the same problem, as you mentioned. The delegate had been set to ViewController, but the viewForAnnotation selector was not being called. After some checks, I realized if you do not call addAnotation in the main thread, mapView would not call viewForAnnotation, so following update resolved my problem:

Before:

[_mMapView addAnnotation:marker];

After:

dispatch_async(dispatch_get_main_queue(), ^{
     [_mMapView addAnnotation:marker];
});
Professionalism answered 8/12, 2013 at 6:43 Comment(4)
This helped me also. Strange that it did not work first time, as it is working the 'before' way in other viewControllers. :)Kolivas
@Kolivas probably in other places your code was running in the UI thread (aka main thread). Glad that helped :-)Professionalism
I had this problem too (Swift). I was trying to update UI from a completion block coming out of NSURLSession.dataTaskWithRequest(). I had to execute my UI updates from the main thread. Thanks @Roozbeh dispatch_async(dispatch_get_main_queue()){self.addMyAnnotation()}Decanal
Thank you very much for the answer .I am running webservice query in asyncDispatchQueue and adding Annotations to mapview in async queue only thats y it doesn't shows up . Now added in GetmainQueue it did trick . wow...Thankssssss..... I am banging from the morning about this issue .Upvoted.Headmistress
S
7

In order to get the viewForAnnotation to be called, add mapView.delegate=self; to e.g. the viewDidLoad method:

- (void)viewDidLoad {

    [super viewDidLoad];
    mapView.delegate=self;
}
Sidky answered 30/12, 2011 at 15:17 Comment(1)
There is a bug in iOS 6 (openradar.appspot.com/12346693) - if you are setting the delegate after the annotations have been added, they will get a default annotation view, and the custom mapView:viewForAnnotation will not be called.Anishaaniso
A
5

Could it be that your annotation has been added outside the current view area of the MKMapView?

Adduction answered 29/9, 2010 at 12:23 Comment(1)
I upvoted this because if you think you've wired everything correctly, double check your latitude and longitude values: Maybe you have them reversed, or have one (or both) be positive when they should be negative. Or, in your code, you're using lat for both lat and lon thanks to a sloppy cut/paste job. =)Haarlem
P
2

For storyboard, Ctl drag the MKMapView to the orange circle on the bottom bar of ViewController, and select delegate.

This will solve the problem.

Prodigy answered 5/2, 2015 at 23:1 Comment(0)
I
2

As vatrif mentioned in the comments, you must set your delegate BEFORE adding annotations to your MKMapView object.

Interrupted answered 30/1, 2016 at 12:28 Comment(0)
H
1

Others have already explained, odds are high you have not connected your mapview delegate to your controller. Its the first thing to check

Hither answered 31/1, 2013 at 11:56 Comment(0)
F
0

i have been working in ios 9 Mapview related app and I experienced the same problem.

somehow I solved my problem, in my case im resizing the mapview. I added delegate after i resize the mapview. it works now perfectly.!

Falcongentle answered 21/7, 2016 at 11:35 Comment(0)
K
0

After having set the delegate for the mapview if still the viewforannotation not getting called then this is something which you have missed - set the self.mapView.showsUserLocation to YES, in interface builder you can tick the shows userLocation option in attributes inspector.

Kendy answered 5/10, 2016 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.