MKPointAnnotation如何隐藏标记图像

问题描述 投票:0回答:2

我有一个已设置的MKPointAnnotation,它显示了我想要的位置,但是它在点上带有默认的大红色别针图标,我想隐藏该图像而不显示任何内容。我已经尝试使用下面用“ *****”注释的部分,并且我认为可能会设置view.image=nil,但这没有任何改变,我也尝试了view.frame.size=CGSize(width: 0, height: 0),但没有任何效果。关于如何完成此操作的其他建议?

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

      let annotation = annotation

      let identifier = "marker"
      var view: MKMarkerAnnotationView


      if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        as? MKMarkerAnnotationView {
        dequeuedView.annotation = annotation
        view = dequeuedView
      } else {
        //************************
        view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        view.frame.size=CGSize(width: 0, height: 0)
        view.canShowCallout = true
        view.calloutOffset = CGPoint(x: -5, y: 5)
        view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)



      }
      return view
    }
swift mapkit mapkitannotation
2个回答
0
投票
这里是关于这个主题的苹果文件:

https://developer.apple.com/documentation/mapkit/mapkit_annotations/annotating_a_map_with_custom_data

他们似乎建议对自定义注释进行编码,如下所示:

class SanFranciscoAnnotation: NSObject, MKAnnotation { // This property must be key-value observable, which the `@objc dynamic` attributes provide. @objc dynamic var coordinate = CLLocationCoordinate2D(latitude: 37.779_379, longitude: -122.418_433) // Required if you set the annotation view's `canShowCallout` property to `true` var title: String? = NSLocalizedString("SAN_FRANCISCO_TITLE", comment: "SF annotation") // This property defined by `MKAnnotation` is not required. var subtitle: String? = NSLocalizedString("SAN_FRANCISCO_SUBTITLE", comment: "SF annotation") }

您可以尝试以这种方式创建自定义注释,使其具有清晰的颜色或大小为(0,0)。将您的视图分配给您编写的自定义注释。

var view: SanFranciscoAnnotation

如果这不起作用,我将从使用自定义注释编写的应用程序中挖掘一些旧代码并共享。

0
投票
实际上这比我预期的要容易解决,我要做的就是将markerTintColor设置为alpha为0的颜色。

view.markerTintColor=UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0,alpha:0.5).withAlphaComponent(0)

在这种情况下是什么为我解决了。
© www.soinside.com 2019 - 2024. All rights reserved.