swift - MapView viewForAnnotation 在圆圈中显示白色图钉(默认)

标签 swift mkmapview

当我没有在 MKMapView 上设置委托(delegate)而是添加注释时,它们的图像如下所示:

Default view

但是,如果我像这样返回一个 MKAnnotationView:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation.isKind(of: MKUserLocation.self) {
        return nil
    }

    guard annotation is MKPointAnnotation else { return nil }

    let identifier = "Annotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView!.collisionMode = .circle
        annotationView!.canShowCallout = true
    } else {
        annotationView!.annotation = annotation
    }

    return annotationView
}

然后图标看起来像一个垂直的图钉:

customised

我知道我可以在 MKAnnotationView 上使用自定义图像。但是,我想要默认的,除了我希望它看起来像第一个而不是第二个,但我也想自定义标注。我怎样才能做到这一点?

最佳答案

第一张图片属于 MKMarkerAnnotationView。所以要求它,而不是 MKPinAnnotationView。

关于swift - MapView viewForAnnotation 在圆圈中显示白色图钉(默认),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55388422/

相关文章:

swift - 使用字典时有人在 Playground 上看到 'some' 输出吗?

ios - 如何使用 Alamofire 重试请求?

iphone - MKMapView -> 为我的位置显示一个按钮

iPhone MkMapView 将中心锁定到用户位置

swift - UIButton 作为 MKAnnotationView 的 subview 不起作用

ios - 如何在 iOS 应用程序上接收连接的 WiFi 模块/路由器发送的数据?

ios - bool 扩展数组

iOS 应用程序不要求位置权限

ios - 注册区域 ios 时是否需要 startLocationUpdates?

ios - 长按快速将图钉注释添加到 map View