2011-03-02 72 views
0

在我的代碼中,- (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

回答

2

你指定一個包含該方法的委託地圖視圖類,你實際上任何註釋添加到地圖視圖,並在部分實際可見的那些註釋的位置當前顯示在屏幕上的地圖?

+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

0

你如何給地圖添加註釋?我用下面的

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

1

有人正確地指出了委託wasnt在我的情況 我所做的就是在代碼中我添加註釋我寫的代碼設置爲同一類:

mapView.delegate=self; 

,它爲我工作大。 如果你沒有做到這一點,它可能也適用於你的機器人。