Swift 3将自定义注释图钉添加到MKMapSnapShotter快照 [英] Swift 3 Add custom annotation pin to MKMapSnapShotter snapshot

查看:253
本文介绍了Swift 3将自定义注释图钉添加到MKMapSnapShotter快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm learning Swift 3 on my own, and my current learning project involves allowing the user to snap a photo and get a map snapshot with the current location pinned.

我依靠此答案从2015年8月开始,此答案从2016年6月开始指导,但我仍然找不到正确的道路.

I've relied on this answer from Aug 2015 and this answer from Jun 2016 for guidance, but I still can't find the right path.

现在,我可以...

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

但是我只是不能把别针固定住.我知道我的代码是不完整和无效的-因此,这不仅仅是调试问题.这就是我一直在使用的东西(以及基于上面链接的许多变体):

But I just can't place the pin. I know that my code is incomplete and ineffective -- so this is more than just a debugging question. Here is what I've been working with (as well as many variations based on the links above):

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

我已经被困了好几天了,所以我当然会很感激.谢谢!

I've been stuck for days now, so I certainly would appreciate any insights. Thanks!

推荐答案

要使用注释视图呈现MKMapSnapshot,您必须在新的图形上下文中手动绘制快照的image和注释视图的image,并从该上下文中获取新图像.在Swift 3中:

To render a MKMapSnapshot with annotation views, you have to manually draw the snapshot's image and the annotation view's image on a new graphic context, and get a new image from that context. In Swift 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视频将MapKit放入透视图.

This was adapted from https://stackoverflow.com/a/18776723/1271826, which itself was adapted from WWDC 2013 video Putting MapKit in Perspective.

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

Here is an example of a snapshot with a .satelliteFlyover:

这篇关于Swift 3将自定义注释图钉添加到MKMapSnapShotter快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆