10

我正在自己学习 Swift 3,我目前的学习项目涉及允许用户拍摄照片并获取固定当前位置的地图快照。

我依靠2015 年 8 月的这个答案2016 年 6 月的这个答案作为指导,但我仍然找不到正确的路径。

此刻,我可以...

  1. 从缓冲区中获取照片
  2. 获取地图快照

但我就是无法放置别针。我知道我的代码不完整且无效——所以这不仅仅是一个调试问题。这是我一直在使用的(以及基于上面链接的许多变体):

let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)

snapShotter.start() {

    snapshot, error in

        guard let snapshot = snapshot else {
            return
        }

        let image = snapshot.image
        let annotation = MKPointAnnotation()
        annotation.coordinate = needleLocation  // is a CLLocationCoordinate2D
        annotation.title = "My Title"

        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotation")

        // I want to push the final image to a global variable
        // can't figure out how to define finalImage as the pinned map
        self.myMap = finalImage


        } // close snapShotter.start

我已经被困了好几天了,所以我当然会很感激任何见解。谢谢!

4

2 回答 2

29

要渲染MKMapSnapshot带有注释的视图,您必须在新的图形上下文中手动绘制快照image和注释视图image,并从该上下文中获取新图像。在斯威夫特 3 中:

let rect = imageView.bounds

let snapshot = MKMapSnapshotter(options: options)
snapshot.start { snapshot, error in
    guard let snapshot = snapshot, error == nil else {
        print(error ?? "Unknown error")
        return
    }

    let image = UIGraphicsImageRenderer(size: options.size).image { _ in
        snapshot.image.draw(at: .zero)

        let pinView = MKPinAnnotationView(annotation: nil, reuseIdentifier: nil)
        let pinImage = pinView.image

        var point = snapshot.point(for: location.coordinate)

        if rect.contains(point) {
            point.x -= pinView.bounds.width / 2
            point.y -= pinView.bounds.height / 2
            point.x += pinView.centerOffset.x
            point.y += pinView.centerOffset.y
            pinImage?.draw(at: point)
        }
    }

    // do whatever you want with this image, e.g.

    DispatchQueue.main.async {
        imageView.image = image
    }
}

这改编自https://stackoverflow.com/a/18776723/1271826,它本身改编自 WWDC 2013 视频Putting MapKit in Perspective

以下是带有 的快照示例.satelliteFlyover

在此处输入图像描述

于 2017-03-13T21:02:09.473 回答
0

我从 Rob 定义“rect”的代码中收到错误,因为我的 MKMapSnapshotter 图像是 UIImage。但是 .bounds 是 UIImageView 的属性,而不是 UIImage。所以 rect 定义会抛出一个错误。

这是我配置选项的方式:

        let mapSnapshotOptions = MKMapSnapshotOptions()

        // Set the region of the map that is rendered.
        let needleLocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region = MKCoordinateRegionMakeWithDistance(needleLocation, 1000, 1000)
        mapSnapshotOptions.region = region

        // Set the scale of the image. We'll just use the scale of the current device, which is 2x scale on Retina screens.
        mapSnapshotOptions.scale = UIScreen.main.scale

        // Set the size of the image output.
        mapSnapshotOptions.size = CGSize(width: 300, height: 300)

        // Show buildings and Points of Interest on the snapshot
        mapSnapshotOptions.showsBuildings = true
        mapSnapshotOptions.showsPointsOfInterest = false

那么还有另一种访问 .bounds 属性的方法吗?还是我错误地创建了我的快照?

于 2017-03-14T19:42:28.487 回答