0

In my code, the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation method is not getting called. I don know why. Can anyone please help me?

Below is my code:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@","];
    NSLog(@"pin map");
    if(pinView == nil) 
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];

        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        UIImage *image = [UIImage imageNamed:@"ann.png"];

        CGRect resizeRect;

        resizeRect.size = image.size;
        CGSize maxSize = CGRectInset(self.view.bounds,
                                     [map annotationPadding],
                                     [map annotationPadding]).size;*/
        maxSize.height -= self.navigationController.navigationBar.frame.size.height + [map calloutHeight];
        if (resizeRect.size.width > maxSize.width)
            resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
        if (resizeRect.size.height > maxSize.height)
            resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

        resizeRect.origin = (CGPoint){0.0f, 0.0f};

        UIGraphicsBeginImageContext(resizeRect.size);
        [image drawInRect:resizeRect];
        UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        pinView.image = resizedImage;
        pinView.opaque = NO;

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self
                        action:@selector(showDetails:)
              forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = rightButton;

        if (annotation == mapView.userLocation)
        {

            return nil;
        }
        return pinView;

    } 
    else 
    {

        pinView.annotation = annotation;
    }

    return pinView;

}

This is how I am adding the annotations to the map:

-(void) Annotations:(int)i
{

    /*NSString* address = [mpartyDetail objectAtIndex:i];
    address = [[address componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: @""];
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", address];
    NSLog(@"nsstring %@",urlString);
    NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
    NSLog(@"location string %@",locationString);
    NSArray *latlng = [locationString componentsSeparatedByString:@","];
    NSLog(@"the latlng %@",latlng);

    float lat = [[latlng objectAtIndex:2] floatValue];
    float lng = [[latlng objectAtIndex:3] floatValue]; */
    NSLog(@"ann");
    lat = [[latiArray objectAtIndex:i]floatValue];
    lng = [[longiArray objectAtIndex:i]floatValue]; 
    CLLocationCoordinate2D newCoord = {lat,lng};
    mapAnnotations* annotation = [[mapAnnotations alloc] initWithCoordinate:newCoord];

    [mapView addAnnotation:annotation];

}

mapAnnotations is another class. The below code shows the method of that class which is getting called:

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate{

    NSLog(@"the cor");
    self = [super init];

    if (self != nil) {
        NSLog(@"in");
        _coordinate = coordinate;

    }

    return self;

}

The idea is that I add annotations when the user presses on the map and the user can give any title to that pin. I need a callout to show the title entered by the user. The annotations are getting added. But I am not able to get the callout as this method is not getting called.

Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
Droidme
  • 1,223
  • 6
  • 25
  • 45

3 Answers3

2

Did you assign the class containing that method as the delegate for your map view, did you actually add any annotations to the map view, and are the locations for those annotations actually visible in the portion of the map currently displayed on-screen?

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • I have edited the code. I have added the method which adds annotations – Droidme Mar 02 '11 at 05:02
  • The annotations are getting added. But I am not able to get the callout as this method is not getting called – Droidme Mar 02 '11 at 05:06
  • 2
    Are you at some point calling `[mapView setDelegate:self]` or `mapView.delegate = self` (assuming `self` is the correct object), or tying the appropriate class to the delegate in Interface Builder? Are you showing the region of the map with your annotations (e.g. if your annotations are all for locations in France but the map view is showing China, the method probably won't be called)? – Anomie Mar 02 '11 at 05:09
  • Yes thanks a lot. I had not set the delegate. But I am not getting the callout. – Droidme Mar 02 '11 at 05:15
  • PLease do help me. I don know y I am not getting the callout – Droidme Mar 02 '11 at 05:25
  • @Anomie As you can see in the method, I have set the pinView.animatesDrop = YES and pinView.canShowCallout = YES. But both they are not working. am I missing naything here. – Droidme Mar 02 '11 at 05:59
  • I did it. Thanks to all for your time and help – Droidme Mar 03 '11 at 06:55
  • This was the solution in my case: "if your annotations are all for locations in France but the map view is showing China, the method probably won't be called" – Vladimir Stazhilov Sep 21 '12 at 09:23
1

as someone rightly pointed out the delegate wasnt set to the same class in my case all i did was in the code where i add annotation i wrote the code:

mapView.delegate=self;

and it worked great for me. if you didnt do that do it and it might work for you also droid.

Griwes
  • 8,805
  • 2
  • 43
  • 70
user623027
  • 11
  • 1
0

How are you adding annotations to your map? I use the following

while(some condition){
MapAnnotation *annotation = [[MapAnnotation alloc] initWithLatitude:(float)goLat Longitude:(float)goLng title:(NSString*)recallTask.blurb subtitle:(NSString*)recallTask.location];
        [appDelegate.annArray addObject:annotation];
        [annotation release];
        annotation = nil;
    }
[self.mapView addAnnotations:appDelegate.annArray];
Robot Woods
  • 5,677
  • 2
  • 21
  • 30
  • Yes I do something similar to that. I have edited the code. I have added the method which adds annotations – Droidme Mar 02 '11 at 05:03
  • The annotations are getting added. But I am not able to get the callout as this method is not getting called – Droidme Mar 02 '11 at 05:07