- (MKAnnotationView *)mapView not getting called

2019-09-09 06:18发布

问题:

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.

回答1:

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?



回答2:

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.



回答3:

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];