- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在为通过 MapKit 添加到我的 map View 中的注释创建标注。注释效果很好,但目前没有显示标注,即使我使用正确的代码来启用它们(我相信)。
这是我的viewFor annotation
代码块。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
if let annotation = annotation as? ClusterAnnotation {
let identifier = "cluster"
return mapView.annotationView(annotation: annotation, reuseIdentifier: identifier)
} else {
let identifier = "pin"
let annotationView = mapView.annotationView(of: MKMarkerAnnotationView.self, annotation: annotation, reuseIdentifier: identifier)
annotationView.isEnabled = true
annotationView.canShowCallout = true
annotationView.accessibilityLabel = "hi"
annotationView.isHidden = false
annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView.markerTintColor = UIColor.customBlue()
annotationView.glyphImage = UIImage(named: "person")
return annotationView
}
}
annotationView
功能的扩展代码块。
extension MKMapView {
func annotationView<T: MKAnnotationView>(of type: T.Type, annotation: MKAnnotation?, reuseIdentifier: String) -> T {
guard let annotationView = dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? T else {
return type.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
annotationView.annotation = annotation
return annotationView
}
}
注释在我选择它时会放大,并且在 didSelect
代码块中它会运行我在其中运行的打印语句。不确定发生了什么,即使我已经启用了几乎所有内容,也不允许显示标注。
最佳答案
请使用此代码。这段代码对我来说很好用。此代码支持 Swift4 和 Swift5。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
if (annotation is MKUserLocation) {
return nil
} else {
let annotationIdentifier = "AnnotationIdentifier"
let nibName = "MyAnnotationView" //My XIB Name
let viewFromNib = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?.first as! MyAnnotationView // get My XIB
var annotationView: MyAnnotationView?
// if there is a view to be dequeued, use it for the annotation
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MyAnnotationView {
if dequeuedAnnotationView.subviews.isEmpty {
dequeuedAnnotationView.addSubview(viewFromNib)
}
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
} else {
// if no views to dequeue, create an Annotation View
let av = MyAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.addSubview(viewFromNib)
annotationView = av // extend scope to be able to return at the end of the func
}
// after we manage to create or dequeue the av, configure it
if let annotationView = annotationView {
annotationView.canShowCallout = true // callout bubble
annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView.frame = CGRect(x: 0, y: 0, width: 75, height: 80)
let customView = annotationView.subviews.first as! MyAnnotationView
customView.frame = annotationView.frame
}
return annotationView
}
}
此代码输出:
很高兴为您提供帮助。
关于ios - MKMarkerAnnotationView 不显示标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58989265/
我一直在尝试新的 iOS 11 MKMarkerAnnotationView,最后集群工作起来就像一个魅力。但问题是,如图所示,簇的颜色错误。我怎样才能控制这个? 最佳答案 没关系,我设法弄清楚了。
我正在尝试缩放大图像以适合 MKMarkerAnnotationView 子类的字形图像: class MyAnnotationView: MKMarkerAnnotationView { v
设置带有附近咖啡馆注释的 map View 。但是当我尝试使用 MKMarkerAnnotationView 自定义注释时, map View 上没有任何显示。当我记录标记时,框架是 (0 0; 0
我目前正在为通过 MapKit 添加到我的 map View 中的注释创建标注。注释效果很好,但目前没有显示标注,即使我使用正确的代码来启用它们(我相信)。 这是我的viewFor annotatio
我想从 MKMarkerAnnotationView 获取 UIImage,但使用以下代码图像被截断(在顶部和底部,请参见屏幕截图) let newAnnotation = SimpleAnnotat
为什么 MKMarkerAnnotationView 标题会消失。有时我们旋转 map 时会出现标题。如何使 MKMarkerAnnotationView 标题始终可见。 最佳答案 MKMarkerA
我有一个 MKMapView,我想在长按手势上添加注释。添加注释后,我想选择注释 View 。我认为这是一个非常简单的请求。 问题是我正在使用带有自定义 rightCalloutAccessoryVi
我尝试在带有标题的图像 (MKSnapshot) 上绘制标记,但标题未显示。 有谁知道为什么会出现这种情况吗? let annotation = MKPointAnnotation() ann
我正在为 iOS 构建一个应用程序,允许用户基于位置存储图像。现在我想为此使用 iOS 中最新的 AnnotationView (MKMarkerAnnotationView),因为它提供了自动集群。
我正在尝试使用添加到 ios11 中的 map View 的新功能。 我正在将我所有的 MKAnnotationView 与圆形碰撞进行聚类,但我必须在注释变为聚类时实时检查。 我不知道该怎么做。 编
我正在尝试像照片应用程序那样做一个自定义 pinView: 到目前为止,我可以在 SO 或 here 上找到的大多数资源和 here显示自定义图钉图像或标注 View 的方法,但在标记本身的自定义 V
我有一个表格 View ,我想在 UITableViewCell 中显示自定义 MKMarkerAnnotationView。该 TableView 显示在专用的 ViewController 中,而
在较旧的手机(iPhone 7、8)上,为 MKMapSnapshotter 创建的 MKMarkerAnnotationView 可以在 map 上正确显示。仅在 iPhone X 及更高版本上它们
我已经通过引用苹果提供的以下示例对点进行了聚类。 https://developer.apple.com/documentation/mapkit/mkannotationview/declutter
我有一个实现 MKAnnotation 协议(protocol)的 MapItem 类。我正在使用 MKMarkerAnnotationView 在 map 上显示注释。 根据文档,MKMarkerA
MKPinAnnotationView 在 iOS 15 中重命名为 MKMarkerAnnotationView。 在 iOS 14 之前,我将 MKPinAnnotationView 子类化为 c
我是一名优秀的程序员,十分优秀!