【问题标题】:Why is my MKPointAnnotation not Custom?为什么我的 MKPointAnnotation 不是自定义的?
【发布时间】:2013-05-12 12:05:01
【问题描述】:

我的 MKPointAnnotation 应该使用此代码是自定义的:

-(MKPointAnnotation*)setAnnotation: (NSString*) title atLocation:(CLLocationCoordinate2D)Location withImage:(UIImage*) LocationImage{
    Pin = [[MKPointAnnotation alloc] init];
    Pin.title = title;
    [Pin setCoordinate:Location];
    [self mapView:mapView viewForAnnotation:Pin].annotation = Pin;
    return Pin;
}



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

        MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];


        if(!pinView){
            pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            pinView.enabled = YES;
            UIButton *PicButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [PicButton addTarget:self action:@selector(showLocationPicture) forControlEvents:UIControlEventTouchUpInside];\
            pinView.rightCalloutAccessoryView = PicButton;
        }
        else{
            pinView.annotation = annotation;
        }
    return pinView;
}

然而,由于某种原因,Pin 仍然是默认的,有人可以帮我吗? 谢谢

【问题讨论】:

    标签: ios objective-c mkmapviewdelegate


    【解决方案1】:

    你不应该自己打电话给viewForAnnotation。您应该只向地图视图添加注释,然后它将确定在任何给定时刻可见的内容并为您调用viewForAnnotation。因此:

    • 鉴于不同的注释可以有不同的图像,你真的想继承MKPointAnnotation 并给它一个imageName 的属性(注意,不是UIImage 本身,而是viewForAnnotation 的名称或标识符可以用来创建UIImage 本身):

      @interface MyAnnotation : MKPointAnnotation
      @property(nonatomic, strong) NSString *imageName;
      @end
      
      @implementation MyAnnotation
      @end
      
    • 向您的地图视图添加注释(不是注释视图),例如:

      - (id<annotation>)addAnnotationWithTitle:(NSString *)title coordinate:(CLLocationCoordinate2D)coordinate imageName:(NSString *)imageName
      {
          MyAnnotation *annotation = [[MyAnnotation alloc] init];
          annotation.title = title;
          annotation.coordinate = coordinate;
          annotation.imageName = imageName;
          [mapView addAnnotation:annotation];
          return annotation;
      }
      

      注意,我不建议将 Pin 设为类 ivar/property,并且我会使用 camelCase 作为变量名(以小写开头),我会将方法名称从 setAnnotation 更改为类似 @ 987654333@等

    • 确保您的控制器已被指定为 mapView 的委托;

    • 将为您调用viewForAnnotation(如果注释可见)。应该是这样的:

      - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
      {
          if ([annotation isKindOfClass:[MKUserLocation class]])
              return nil;
      
          if ([annotation isKindOfClass:[MyAnnotation class]])
          {
              MyAnnotation *customAnnotation = annotation;
      
              static NSString *annotationIdentifier = @"MyAnnotation";
              MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
              if (!annotationView)
              {
                  annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
                  annotationView.canShowCallout = YES;
                  annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
              }
              else
              {
                  annotationView.annotation = annotation;
              }
      
              annotationView.image = [UIImage imageNamed:customAnnotation.imageName];
      
              return annotationView;
          }
      
          return nil;
      }
      

      注意,animatesDrop 是一个 MKPinAnnotationView 选项,您不能使用自定义注释视图。但是,如果您愿意,很容易实现(参见How can I create a custom "pin-drop" animation using MKAnnotationView?)。我建议你先处理基本的注解视图,然后再看自定义注解视图的拖放动画。

    【讨论】:

    • 那么我该如何调整注解的设置呢?因为现在我使用类似的东西: pinView.rightCalloutAccessoryView = PicButton;但这也不起作用
    • @BlackMagic 使用更完整的代码示例查看我修改后的答案。
    • 非常感谢,这正是我所需要的。
    猜你喜欢
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多