- (MKAnnotationView *)mapView 没有被调用 [英] - (MKAnnotationView *)mapView not getting called
问题描述
在我的代码中,没有调用 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id
方法.我不知道为什么.有人可以帮我吗?
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?
下面是我的代码:
- (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 是另一个类.下面的代码显示了被调用的那个类的方法:
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.
推荐答案
您是否将包含该方法的类指定为地图视图的代理,您是否真的向地图视图添加了任何注释,以及这些注释的位置在当前显示在屏幕上的地图部分中实际可见的注释?
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?
这篇关于- (MKAnnotationView *)mapView 没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!