首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MKMapSnapshotter上绘制CLLocationCoordinate2Ds (在mapView打印图像上绘制)

如何在MKMapSnapshotter上绘制CLLocationCoordinate2Ds (在mapView打印图像上绘制)
EN

Stack Overflow用户
提问于 2019-02-14 03:50:16
回答 1查看 832关注 0票数 1

我有一个带有CLLocationCoordinate2D数组的mapView。我使用MKPolyline在我的mapView上使用这些位置来绘制线条。现在,我想将其存储为UIimage。我发现有MKMapSnapshotter类,但不幸的是我不能在它上面绘制覆盖图“快照对象不能捕获你的应用程序创建的任何覆盖图或注释的可视表示。”所以我只能得到空白的地图图像。有没有办法用我的覆盖图获取图像?

代码语言:javascript
复制
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...
        }
    }
}

如何在此图像上放置叠加层?或者可能有其他的方法来解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-14 05:15:17

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

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

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54678314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档