Using MapKit in an XCode Playground

I saw some outdated material about using MapKit in an XCode Playground, so I thought I’d post an example for XCode 7.1.1 and Swift 2.1. Being able to access and manipulate a map this easily if freaking amazing. This code allows you to view the map in the timeline on the right-hand pane. You can press the diagonal Venn diagram at the top right-hand corner to toggle the timeline.

import MapKit
import XCPlayground

// set the zoom
let delta = 1.75

// set the size of the map
let frame = CGRect( x:0, y:0, width:200, height:200 )
let mapView = MKMapView( frame: frame )

// create and populate a coordinate region struct
var region = MKCoordinateRegion()
region.center.latitude = 32.0
region.center.longitude = -102.0

// span defines the zoom
region.span.latitudeDelta = delta
region.span.longitudeDelta = delta

// inform the mapView of these edits
mapView.setRegion( region, animated: true )

// view the map in the timeline!
XCPlaygroundPage.currentPage.liveView = mapView

At the end of the day, you should see something like this:

Leave a Reply

Your email address will not be published. Required fields are marked *