首页 > 解决方案 > 自定义注释标注

问题描述

我正在尝试制作注释调用框,因为它没有颜色并且完全透明。

代码如下:

import Foundation
import MapKit

class CustomPin: NSObject, MKAnnotation
{     
    var coordinate: CLLocationCoordinate2D
    var title: String?

    init(pinTitle: String, location: CLLocationCoordinate2D) {
        self.title = pinTitle
        self.coordinate = location
    }
}

func setPin(location: CLLocationCoordinate2D)
{
    let pin = CustomPin(pinTitle:"" ,location: location)

    self.mapView.addAnnotation(pin)
    self.mapView.delegate = self
    print(location.latitude)
    print(location.longitude)
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
{   
    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customAnnotation")
    annotationView.image = UIImage(named: "marker")
    annotationView.canShowCallout = true
    annotationView.detailCalloutAccessoryView = addressLabel

    return annotationView
}

结果如下所示:

我的应用

期望的结果应该是这样的:

在此处输入图像描述

有什么要修改的?

标签: swiftmapkitmkannotationmkannotationviewmapkitannotation

解决方案


我看了一点,我认为除了修改左右附件视图之外,Apple 没有任何你可以直接对标注外观做的事情。我认为您必须创建自己的自定义标注。

如果您尝试制作自己的自定义标注,请查看:https ://github.com/robertmryan/CustomMapViewAnnotationCalloutSwift

您还可以将此 Cocoapod 用于自定义标注:https ://github.com/okhanokbay/MapViewPlus


推荐阅读