簡體   English   中英

Swift 3將自定義注釋引腳添加到MKMapSnapShotter快照

[英]Swift 3 Add custom annotation pin to MKMapSnapShotter snapshot

我正在學習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

我已經被困了幾天了,所以我當然會欣賞任何見解。 謝謝!

要使用注釋視圖渲染MKMapSnapshot ,您必須在新的圖形上下文中手動繪制快照的image和注釋視圖的image ,並從該上下文中獲取新圖像。 在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視頻Putting MapKit in Perspective

以下是帶有.satelliteFlyover的快照.satelliteFlyover

在此輸入圖像描述

我從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屬性的方法嗎? 或者我是否錯誤地創建了快照?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM