Skip to content

Instantly share code, notes, and snippets.

@ajaysinghthakur
Created May 21, 2016 07:44
Show Gist options
  • Save ajaysinghthakur/edac9ca7666dc37b7d8b043d8398bf75 to your computer and use it in GitHub Desktop.
Save ajaysinghthakur/edac9ca7666dc37b7d8b043d8398bf75 to your computer and use it in GitHub Desktop.
adding detail callout accesory view to mkannonationview to show map snapshot
func configureDetailView(annotationView: MKAnnotationView) {
let width = 300
let height = 200
let snapshotView = UIView()
let views = ["snapshotView": snapshotView]
snapshotView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[snapshotView(300)]", options: [], metrics: nil, views: views))
snapshotView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[snapshotView(200)]", options: [], metrics: nil, views: views))
self.takeSnapshot(mapView , annotationView: annotationView) { (image, error) in
let imageview = UIImageView.init(image: image)
snapshotView.addSubview(imageview)
}
annotationView.detailCalloutAccessoryView = snapshotView
}
// Takes a snapshot and calls back with the generated UIImage
func takeSnapshot(mapView: MKMapView,annotationView: MKAnnotationView, withCallback: (UIImage?, NSError?) -> ()) {
let options = MKMapSnapshotOptions()
//options.region = mapView.region
options.size = mapView.frame.size
options.mapType = .SatelliteFlyover
options.scale = UIScreen.mainScreen().scale
options.camera = MKMapCamera(lookingAtCenterCoordinate: annotationView.annotation!.coordinate, fromDistance: 250, pitch: 65, heading: 0)
let snapshotter = MKMapSnapshotter(options: options)
snapshotter.startWithCompletionHandler() { snapshot, error in
guard snapshot != nil else {
withCallback(nil, error)
return
}
withCallback(snapshot!.image, nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment