swift 如何将 kml 文件中的图标添加到 mapkit

How to add icon from kml file to mapkit in swift

我有一个 kml 文件:

在地图工具包上显示 kml 文件中的所有数据后就可以了。但在 kml 文件中我无法显示图标。

这是 kml 文件中的图标:

这是注释代码:

open class KMLDocument: KMLElement {
     open var overlays: [MKOverlay] = []
     open var annotations: [KMLAnnotation] = []
     open var styles: [String: KMLStyle] = [:]
     open var placemarks: [KMLPlacemark] = []

     public convenience init(_ element: AEXMLElement, generateMapKitClasses: Bool) {
        self.init(element)
        if generateMapKitClasses {
            initOverlay()
            initAnnotation()
        }
    }

    fileprivate func initAnnotation() {
        for pointPlacemark: KMLPlacemark in placemarks {
            if let point: KMLPoint = pointPlacemark.point {
                let annotation = KMLAnnotation(point.coordinates)
                annotation.title = pointPlacemark.name
                annotation.subtitle = pointPlacemark.description
                annotation.style = pointPlacemark.style
                self.annotations.append(annotation)
            }
        }
    }
}

为了使 KML 文件中的图标图像能够在 Google 地球或地图中正确显示,图标图像需要可公开访问。当我将您的图标图像的 URL 放入我的浏览器 (https://www.svgimages.com/svg-image/s5/send-file-256x256.png) 时,我只是黑屏并且没有图像。因此,无论出于何种原因,URL 都没有返回有效的、可公开访问的图像文件。它可能在 KML 查看器中工作正常,该查看器具有对该服务器的特殊访问权限并且可以获得图像,但可能无法在其他任何地方工作。您可能还想检查它实际上返回的是 PNG 文件,而不是 SVG 文件(因为 URL 在两个地方包含 "svg"),因为 KML 仅支持 JPG、PNG 和 GIF 图像(加上用于叠加的 TIFF 和 GeoTiff)。