Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a Map with MapKit in a Swift playground for a Quick Look?

Tags:

Good to see a tag for swift-playground - love to see this continue.

I've been tinkering with Swift and am wondering whether the MapKit can be used in the playground so that you can use the Quick Look feature so I can iterate and play around with a preview of my work. I haven't figured out the syntax so thought I would ask if anyone has explored this in a playground environment. I am thinking I need some code to establish it as a view controller.

import UIKit import MapKit  // Sample UIView code which works great in the playground. //var myView:UIView = UIView(); //myView.frame = CGRectMake(0, 0, 320, 560) //myView.backgroundColor = UIColor.blueColor()  // Non working map code - no errors but can't figure out what to call or initiate to get the view in the Quick Look side.  var mapView = MKMapView(); mapView.region.center.latitude = mapView.userLocation.coordinate.latitude; mapView.region.center.longitude = mapView.userLocation.coordinate.longitude; mapView.region.span.latitudeDelta = 0.00725; mapView.region.span.longitudeDelta = 0.00725; 

Guessing I am just missing something simple for the mapView to init itself etc. I love the interpreted playground area for tinkering around, just need to figure out if it can do mao functionality as opposed to just writing .swift files and environment within Xcode and getting more formal.

like image 504
Kokanee Avatar asked Jun 09 '14 06:06

Kokanee


People also ask

How do I use MapKit in swift 5?

Go to the storyboard. Drag a MapKit View to the main View. Give the MapKit View the same size as the main View. Select the MapKit View and go to the Pin button from the Auto Layout button on the bottom-right of the Storyboard and fill in the following values.


1 Answers

For XCode7.1 and Swift2.1 on OS X, I opened the timeline and did the following:

import MapKit import XCPlayground  let delta = 5.0 let frame = CGRect( x:0, y:0, width:200, height:200 ) let mapView = MKMapView( frame: frame ) var region = MKCoordinateRegion() region.center.latitude = 31.0 region.center.longitude = -110.0 region.span.latitudeDelta = delta region.span.longitudeDelta = delta mapView.setRegion( region, animated: true )  XCPlaygroundPage.currentPage.liveView = mapView XCPlaygroundPage.currentPage.needsIndefiniteExecution = true 

Xcode 9.x, Swift 4.1 update:

import PlaygroundSupport import MapKit

let delta = 5.0 let frame = CGRect( x:0, y:0, width:200, height:200 ) let mapView = MKMapView( frame: frame ) var region = MKCoordinateRegion() region.center.latitude = 31.0 region.center.longitude = -110.0 region.span.latitudeDelta = delta region.span.longitudeDelta = delta mapView.setRegion( region, animated: true )  PlaygroundPage.current.liveView = mapView PlaygroundPage.current.needsIndefiniteExecution = true 
like image 144
cjohnson318 Avatar answered Sep 22 '22 02:09

cjohnson318