【问题标题】:didSelectAnnotationView called automatically when custom annotations are added to mapView将自定义注释添加到 mapView 时自动调用 didSelectAnnotationView
【发布时间】:2016-01-22 16:16:49
【问题描述】:

我正在尝试在 MKMapView 上添加自定义注释,并在点击注释时实现自定义 callOut view

我正在关注this 链接。问题是当我添加自定义注释 didSelectAnnotationView 时,它会自行调用并显示 popOver callout,即使用户没有单击注释

这是我的代码:

    -(void)callAddAnnotationsLocal{
    [_mapView removeAnnotations:[_mapView annotations]];

    CLLocationCoordinate2D coord = {.latitude =  43.3998170679, .longitude =  -95.1288472486};

     [_mapView addAnnotations:[self annotationsLocal]];

}

    -(NSArray *)annotationsLocal{
    self.annotArrayLocal = nil;
    self.annotArrayLocal = [[NSMutableArray alloc] init];
    for (int i=0; i<[arrAmmenities count];i++)
    {
        MKAnnotationView *propertyAnnotation = [[MKAnnotationView alloc] init];
        UIFont *font = [UIFont fontWithName:@"Arial" size:18];
        NSDictionary *userAttributes = @{NSFontAttributeName: font,
                                         NSForegroundColorAttributeName: [UIColor blackColor]};
        NSString *latStr = [NSString stringWithFormat:@"%@",[[arrAmmenities  objectAtIndex:i]objectForKey:@"latitude"]];
        float latitude = [latStr floatValue];
        NSString *longStr = [NSString stringWithFormat:@"%@",[[arrAmmenities  objectAtIndex:i]objectForKey:@"longitude"]];
        float longitude = [longStr floatValue];
        CLLocationCoordinate2D coord = {.latitude =  latitude, .longitude =  longitude};
        PinAnnotation * annotation = [[PinAnnotation alloc] init];
        [annotation setCoordinate:coord];
        [annotation setType:[[arrAmmenities objectAtIndex:i] objectForKey:@"category"]];
        [annotation setTag:i];
        [self.mapView addAnnotation:annotation];

        [self.annotArrayLocal addObject:propertyAnnotation];
    }
    return _annotArrayLocal;

}

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
    MKAnnotationView *annotationView;
    NSString *identifier;

        if ([annotation isKindOfClass:[PinAnnotation class]]) {
            // Pin annotation.
            identifier = @"Pin";
            annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

                annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

                if([[(PinAnnotation *)annotation type] isEqualToString:@"Gas Stations"])
                    annotationView.image = [UIImage imageNamed:@"marker_gas"];
                else if([[(PinAnnotation *)annotation type] isEqualToString:@"Golf Courses"])
                    annotationView.image = [UIImage imageNamed:@"marker_golf"];
                else if([[(PinAnnotation *)annotation type] isEqualToString:@"Restaurants"])
                    annotationView.image = [UIImage imageNamed:@"marker_restaurant"];
                else
                    annotationView.image = [UIImage imageNamed:@"marker_hospital"];

            annotationView.canShowCallout = NO;
        }
    return annotationView;

}



 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
//    [mapView deselectAnnotation:view.annotation animated:YES];

    MapAnnotViewController *controller = [[UIStoryboard storyboardWithName:@"MapStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"sidMapAnnotVC"];
//    controller.annotation = view.annotation; // it's useful to have property in your view controller for whatever data it needs to present the annotation's details

    wyPopController = [[WYPopoverController alloc] initWithContentViewController:controller];
    wyPopController.delegate = self;
    [wyPopController setPopoverContentSize:CGSizeMake(200.0, 100.0)];

    [wyPopController presentPopoverFromRect:view.frame
                                  inView:view.superview
                permittedArrowDirections:WYPopoverArrowDirectionAny
                                animated:YES];
}

我哪里错了?我该如何解决?

【问题讨论】:

    标签: ios mkmapview mkannotationview


    【解决方案1】:

    一些想法:

    1. annotationsLocal 中,您不仅要实例化PinAnnotation 对象并将它们添加到映射中,还要实例化MKAnnotationView 对象,构建它们的数组,然后返回该数组,然后添加该数组作为地图视图的注释。

      MKAnnotationView 的第二个数组根本不应该构建,当然也不应该作为地图视图的注释添加。不要将注解对象(表示地图上点的坐标)和注解视图对象(表示这些注解在地图上呈现时的视觉表示)混为一谈。

    2. 顺便说一句,与您的问题无关,您也在viewForAnnotation 中实例化不必要的注释视图。您应该将注解视图出列,并且仅在您没有成功检索要重用的视图时才实例化一个新视图:

      - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
          MKAnnotationView *annotationView;
      
          if ([annotation isKindOfClass:[PinAnnotation class]]) {
              // Pin annotation.
              NSString *identifier = @"Pin";
              annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
              if (annotationView) {
                  annotationView.annotation = annotation
              } else {
                  annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                  annotationView.canShowCallout = NO;
              }
      
              if ([[(PinAnnotation *)annotation type] isEqualToString:@"Gas Stations"])
                  annotationView.image = [UIImage imageNamed:@"marker_gas"];
              else if([[(PinAnnotation *)annotation type] isEqualToString:@"Golf Courses"])
                  annotationView.image = [UIImage imageNamed:@"marker_golf"];
              else if([[(PinAnnotation *)annotation type] isEqualToString:@"Restaurants"])
                  annotationView.image = [UIImage imageNamed:@"marker_restaurant"];
              else
                  annotationView.image = [UIImage imageNamed:@"marker_hospital"];
          }
      
          return annotationView;
      }
      

    但是我在这里看不到任何会导致调用didSelectAnnotationView 的东西,除非将注释视图添加为注释的错误过程具有导致此问题的一些奇怪的副作用。如果您仍然看到didSelectAnnotationView 被调用,我可能会建议在那里添加一个断点并查看调用堆栈,看看您是否可以看到它被调用的位置。但希望annotationsLocal 的修复能够修复它。

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多