【问题标题】:How to draw a CLLocationCoordinate2Ds on MKMapSnapshotter (drawing on mapView printed image)如何在 MKMapSnapshotter 上绘制 CLLocationCoordinate2Ds(在 mapView 打印图像上绘制)
【发布时间】:2019-02-13 19:50:15
【问题描述】:

我有带有 CLLocationCoordinate2D 数组的 mapView。我使用这些位置通过MKPolyline 在我的mapView 上画线。现在我想将它存储为 UIimage。我发现有 MKMapSnapshotter 类,但不幸的是我无法在其上绘制叠加层“Snapshotter 对象不会捕获您的应用创建的任何叠加层或注释的视觉表示。”所以我只得到空白地图图像。有什么方法可以用我的叠加层获取图像?

private func generateImageFromMap() {
    let mapSnapshotterOptions = MKMapSnapshotter.Options()
    guard let region = mapRegion() else { return }
    mapSnapshotterOptions.region = region
    mapSnapshotterOptions.size = CGSize(width: 200, height: 200)
    mapSnapshotterOptions.showsBuildings = false
    mapSnapshotterOptions.showsPointsOfInterest = false


    let snapShotter = MKMapSnapshotter(options: mapSnapshotterOptions)
    snapShotter.start() { snapshot, error in
        guard let snapshot = snapshot else {
//do something with image .... 
            let mapImage = snapshot...
        }
    }
}

如何在此图像上添加叠加层?或者也许还有其他方法可以解决这个问题。

【问题讨论】:

    标签: ios swift mapkit mkmapsnapshotter


    【解决方案1】:

    不幸的是,您必须自己绘制它们。幸运的是,MKSnapshot 有一个方便的point(for:) 方法可以将快照中的CLLocationCoordinate2D 转换为CGPoint

    例如,假设您有一个 CLLocationCoordinate2D 数组:

    private var coordinates: [CLLocationCoordinate2D]?
    
    private func generateImageFromMap() {
        guard let region = mapRegion() else { return }
    
        let options = MKMapSnapshotter.Options()
        options.region = region
        options.size = CGSize(width: 200, height: 200)
        options.showsBuildings = false
        options.showsPointsOfInterest = false
    
        MKMapSnapshotter(options: options).start() { snapshot, error in
            guard let snapshot = snapshot else { return }
    
            let mapImage = snapshot.image
    
            let finalImage = UIGraphicsImageRenderer(size: mapImage.size).image { _ in
    
                // draw the map image
    
                mapImage.draw(at: .zero)
    
                // only bother with the following if we have a path with two or more coordinates
    
                guard let coordinates = self.coordinates, coordinates.count > 1 else { return }
    
                // convert the `[CLLocationCoordinate2D]` into a `[CGPoint]`
    
                let points = coordinates.map { coordinate in
                    snapshot.point(for: coordinate)
                }
    
                // build a bezier path using that `[CGPoint]`
    
                let path = UIBezierPath()
                path.move(to: points[0])
    
                for point in points.dropFirst() {
                    path.addLine(to: point)
                }
    
                // stroke it
    
                path.lineWidth = 1
                UIColor.blue.setStroke()
                path.stroke()
            }
    
            // do something with finalImage
        }
    }
    

    然后是以下地图视图(坐标,如MKPolyline,由mapView(_:rendererFor:) 渲染,像往常一样):

    上面的代码会创建这个finalImage:

    【讨论】:

    • 不知道为什么,但没有使用私有变量坐标:[CLLocationCoordinate2D]?对我来说。我无法将任何 2C 坐标附加到这个数组。我将其更改为 var coord2d: [CLLocationCoordinate2D] = [] 并更改保护让让坐标 = self.coord2d if self.coord2d.count > 1 { } else { return }。而且效果很好!这就是我一直在寻找的
    • 没问题。我很高兴这解决了你的问题。 FWIW,该私有变量的使用与这里的问题无关。显然,我假设您会使用自己的现有坐标数组。但是你没有分享这些坐标是如何存储在你的代码中的,所以我不得不使用一些东西,而且我无法知道你的数组是什么。大声笑。
    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多