【问题标题】:Getting wrong annotation from MapView Delegate method从 MapView 委托方法获取错误注释
【发布时间】:2011-03-10 22:44:22
【问题描述】:

我在我的 .h 文件中声明了以下内容:

Annotation *annoForMoreDetails

但是当我尝试将其设置为方法中的当前注释时

- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation> )annotation

它被设置为错误的对象。

这是我的代码:

 - (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation> )annotation
{
    NSLog(@"welcome into the map view annotation");
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    for (annotation in [mapView annotations]) {
        if ([[mapView annotations] containsObject:annotation]) {
            annoForMoreDetails = annotation;
        }
    }
    if (annoForMoreDetails.coordinate.latitude != mapViews.userLocation.coordinate.latitude && annoForMoreDetails.coordinate.longitude != mapViews.userLocation.coordinate.longitude) {
    // try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    pinView.pinColor = MKPinAnnotationColorGreen;

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(moreDetails)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;
    return pinView;
    }
    else if (annoForMoreDetails.coordinate.latitude == mapViews.userLocation.coordinate.latitude && annoForMoreDetails.coordinate.longitude == mapViews.userLocation.coordinate.longitude) {
        return nil;
    }
    return nil;
}

- (void)moreDetails {
    annotationCalloutView.frame = CGRectMake(70, 120, 188, 218);
    annotationCalloutView.alpha = 0.0;
    mapView.userInteractionEnabled = NO;
    annotationCalloutView.userInteractionEnabled = YES;
    titleLabel.text = annoForMoreDetails.title;
    titleLabel.numberOfLines = 0;
    titleLabel.userInteractionEnabled = NO;
    addressLabel.text = annoForMoreDetails.subtitle;
    [self.view addSubview:annotationCalloutView];
    [UIView beginAnimations:@"callout" context:NULL];
    [UIView setAnimationDuration:0.6];
    annotationCalloutView.alpha = 1.0;
    [UIView commitAnimations];
}

如果发现有问题请指出!

【问题讨论】:

  • 您的annoForMoreDetails 对象实际上是Annotation* 类型,还是类似于id&lt;MKAnnotation&gt;? (应该是后者。)
  • 它是 NSObject 的子类,MKAnnotation 在 "" 中。 So 类型的 Annotation*

标签: iphone objective-c ios4 mkmapview


【解决方案1】:

您是否尝试使用 annoForMoreDetails 来了解用户点击了哪个注释,以便显示更多详细信息?如果是这样的话,还有更简单的方法。使用地图视图的 calloutAccessoryControlTapped 委托方法。当用户点击附件按钮时会调用它,它会传递包含注释作为属性的注释视图。

删除 annoForMoreDetails 实例变量并在 viewForAnnotation 中删除设置 annoForMoreDetails 的 for 循环(我认为它最终每次都将其设置为最后一个注释)。删除所有其他对 annoForMoreDetails 的引用。

同样在 viewForAnnotation 中,删除 rightButton 上的 addTarget 行,因为您将使用 calloutAccessoryControlTapped 的实现替换自定义的 moreDetails 方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control
{
    Annotation *annoForMoreDetails = (Annotation *)view.annotation;

    //the code currently in moreDetails method goes here...
}

【讨论】:

  • 非常感谢!它工作得很好,你完全明白我想要什么!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多