- (MKAnnotationView *)mapView未被调用

问题描述:

在我的代码中,- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation方法没有被调用。我不知道为什么。谁能帮帮我吗?- (MKAnnotationView *)mapView未被调用

下面是我的代码:

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

} 

这是怎么了添加注释到地图:

-(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是另一个类。下面的代码显示了获取调用这个类的方法:

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate{ 

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

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

    } 

    return self; 

} 

的想法是,我添加注释时,在地图上用户按下,并且用户可以给任何标题到脚。我需要一个标注来显示用户输入的标题。注释正在被添加。但是由于这个方法没有被调用,我无法得到标注。

+0

您是否在.h文件中设置了委托MKAnnotation? – dks1725 2011-03-02 04:55:18

+0

@ dks1725是的,我已经设置了代表 – Droidme 2011-03-02 05:03:47

你指定一个包含该方法的委托地图视图类,你实际上任何注释添加到地图视图,并在部分实际可见的那些注释的位置当前显示在屏幕上的地图?

+0

我已编辑了代码。我添加了添加注释的方法 – Droidme 2011-03-02 05:02:53

+0

注释正在被添加。但是我无法得到标注,因为这种方法没有被调用 – Droidme 2011-03-02 05:06:23

+2

你是否在某种程度上调用'[mapView setDelegate:self]'或'mapView.delegate = self'(假设'self'是正确的对象),或者将相应的类绑定到Interface Builder中的委托中?您是否使用注释显示了地图的区域(例如,如果您的注释全部针对法国的地点,但地图视图显示的是中国,该方法可能不会被调用)? – Anomie 2011-03-02 05:09:29

你如何给地图添加注释?我用下面的

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

是的,我做了类似的事情。我编辑了代码。我添加了添加注释的方法 – Droidme 2011-03-02 05:03:09

+0

注释正在增加。但我无法获得标注,因为此方法未被调用 – Droidme 2011-03-02 05:07:32

有人正确地指出了委托wasnt在我的情况 我所做的就是在代码中我添加注释我写的代码设置为同一类:

mapView.delegate=self; 

,它为我工作大。 如果你没有做到这一点,它可能也适用于你的机器人。