在我的代碼中,- (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;
}
的想法是,我添加註釋時,在地圖上用戶按下,並且用戶可以給任何標題到腳。我需要一個標註來顯示用戶輸入的標題。註釋正在被添加。但是由於這個方法沒有被調用,我無法得到標註。
您是否在.h文件中設置了委託MKAnnotation? – dks1725 2011-03-02 04:55:18
@ dks1725是的,我已經設置了代表 – Droidme 2011-03-02 05:03:47