【问题标题】:- (MKAnnotationView *)mapView not getting called- (MKAnnotationView *)mapView 没有被调用
【发布时间】:2011-03-02 04:45:54
【问题描述】:

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

下面是我的代码:

- (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 是的,我已经设置了代理

标签: iphone map annotations callouts


【解决方案1】:

您是否将包含该方法的类指定为您的地图视图的委托,您是否确实向地图视图添加了任何注释,以及这些注释的位置是否在当前显示在屏幕上的地图部分中实际可见?

【讨论】:

  • 我已经编辑了代码。我添加了添加注释的方法
  • 正在添加注释。但我无法获得标注,因为没有调用此方法
  • 您是在某个时候调用[mapView setDelegate:self]mapView.delegate = self(假设self 是正确的对象),还是将适当的类绑定到Interface Builder 中的委托?您是否使用注释显示地图区域(例如,如果您的注释都是针对法国的位置,但地图视图显示的是中国,则可能不会调用该方法)?
  • 是的,非常感谢。我没有设置委托。但我没有得到标注。
  • 请帮帮我。我不知道你没有得到标注
【解决方案2】:

正如有人正确指出,在我的情况下,委托未设置为同一类 我所做的只是在我添加注释的代码中我编写了代码:

mapView.delegate=self;

它对我很有用。 如果你不这样做,那么它可能对你也有用。

【讨论】:

    【解决方案3】:

    您是如何向地图添加注释的?我使用以下

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

    【讨论】:

    • 是的,我做了类似的事情。我已经编辑了代码。我添加了添加注释的方法
    • 正在添加注释。但我无法获得标注,因为没有调用此方法
    猜你喜欢
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    相关资源
    最近更新 更多