【问题标题】:Mapkit - Change annotation image on tappingMapkit - 点击时更改注释图像
【发布时间】:2011-11-05 22:09:21
【问题描述】:

我是 iphone 开发新手。我在地图上添加了注释。我能够在 didSelectAnnotationView() 中捕获注释上的点击事件。但是我想在用户点击注释时更改注释的图像。

【问题讨论】:

    标签: iphone annotations mapkit


    【解决方案1】:

    斯威夫特 3: 我假设您已经在自定义 MKAnnotation 视图中使用此函数

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {...}
    

    我会将 @djibouti33 的答案从 Objective-c 更新为 Swift 3:

        func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
            view.image = UIImage(named: "unselected_marker")
            view.frame = CGRect(x: 0, y: 0, width: 40, height: 40)//keep the new icon size resobable.
         }
    
        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
            view.image = UIImage(named: "selected_marker")
            view.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        }
    

    【讨论】:

      【解决方案2】:

      如果您使用的是MKAnnotationView 而不是MKPinAnnotationView,您可以只依赖MKMapViewDelegate 方法:

      - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
      {
          view.image = [UIImage imageNamed:@"selected_image"];
      }
      
      - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
      {
          view.image = [UIImage imageNamed:@"deselected_image"];
      } 
      

      【讨论】:

        【解决方案3】:

        像这样设置图像属性。

        annView.image = [UIImage imageNamed:@"AnnotationIcon.png"];
        

        编辑

        所以,你似乎在使用MKPinAnnotationView 它有一个pinColor 属性。

        因此,你可以这样改变它

        pin.pinColor = MKPinAnnotationColorRed; // green and purple are 2 other colors.
        

        【讨论】:

        • 当我单击注释时,注释图像会发生变化,但是当我再次单击选定的杆时,我想将图像重置为绿色。有没有办法实现这个?仅单击一次的注释应保持红色。
        猜你喜欢
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 2021-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-08
        相关资源
        最近更新 更多