Skip to content

Commit

Permalink
Print annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev1an committed Sep 4, 2017
1 parent 4ecd10f commit 3b7dde7
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions eidReader/PrintableMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ class PrintableMapView: MKMapView {
} else {
if let context = NSGraphicsContext.current() {
let options = MKMapSnapshotOptions()
let pitch = 1.5
let span = MKCoordinateSpan(latitudeDelta: region.span.latitudeDelta/pitch, longitudeDelta: region.span.longitudeDelta/pitch)
options.region = MKCoordinateRegion(center: region.center, span: span)
options.size.width = dirtyRect.width * 2 * CGFloat(pitch)
options.size.height = dirtyRect.height * 2 * CGFloat(pitch)
options.showsBuildings = true
let pitch: CGFloat = 3.0
options.region = region
options.size.width = dirtyRect.width * pitch
options.size.height = dirtyRect.height * pitch

let mapSnapshotter = MKMapSnapshotter(options: options)

let semaphore = DispatchSemaphore(value: 0)
mapSnapshotter.start(with: DispatchQueue.global(qos: .userInteractive)) { (snapshot, error) -> Void in
mapSnapshotter.start(with: DispatchQueue.global(qos: .userInitiated)) { (snapshot, error) -> Void in
// do error handling
if let snapshot = snapshot {
NSGraphicsContext.setCurrent(context)
Expand All @@ -35,13 +33,29 @@ class PrintableMapView: MKMapView {
t.scaleX(by: 1, yBy: -1)
t.concat()
snapshot.image.draw(in: dirtyRect)

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

let t2 = NSAffineTransform()
t2.scale(by: 1.0/pitch)
t2.concat()

for annotation in self.annotations {
var point = snapshot.point(for: annotation.coordinate)
point.x -= pinView.bounds.size.width / 2 / pitch
point.y -= pinView.bounds.size.height / 2 / pitch
point.x += pinCenter.x / pitch
point.y += pinCenter.y / pitch
pinImage?.draw(at: point, from: dirtyRect, operation: .sourceOver, fraction: 1)
}

Swift.print("map drawn")
semaphore.signal()
}
}
semaphore.wait()
Swift.print("end of draw func")

}
}
}
Expand Down

0 comments on commit 3b7dde7

Please sign in to comment.