24

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Kokanee
  • 975
  • 4
  • 9
  • 20
  • http://stackoverflow.com/questions/24108093/how-to-reference-swift-playground-itself?lq=1 suggests that playground aren't interactive. – Maria Zverina Jun 09 '14 at 10:15
  • Maybe this tutorial would help you figure out your issue: http://www.appshocker.com/swift-programming-ios-101-part-4-mapkit-app-displaying-locations/ – antonio-gomez Jun 11 '14 at 07:14
  • actually it did show errors in the error console when add a line with just mapView but I can't google solution for them. and I've also tried XCPShowView("Map", mapView) with no luck – ReDetection Jul 29 '14 at 03:47
  • Did you ever resolve this? – Jeef Nov 19 '14 at 15:52

3 Answers3

8

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
Adrian
  • 16,233
  • 18
  • 112
  • 180
cjohnson318
  • 3,154
  • 30
  • 33
  • This seems to *almost* work, I get the map, grid and it zooms into a point, but then it just loads a tan square (as if I'm in a desert). Changing the map type to Satellite or Hybrid doesn't change anything either. – Nilloc Nov 16 '15 at 21:45
  • 1
    @Nilloc I was able to get this to work by adding the line `XCPlaygroundPage.currentPage.needsIndefiniteExecution = true` after setting the `liveView` property. – Ziewvater Dec 22 '15 at 20:49
  • @Ziewvater thanks for the edit! I wonder why it worked out okay for me a while back – cjohnson318 Dec 22 '15 at 21:17
4

Try this:

import UIKit
import MapKit
import XCPlayground // Required for XCPShowView

let frame = CGRect(x: 0, y: 0, width: 150, height: 150)
let mapView = MKMapView(frame: frame)
mapView.region.center.latitude = 37.3347606
mapView.region.center.longitude = -122.0548883
mapView.region.span.latitudeDelta = 0.00725
mapView.region.span.longitudeDelta = 0.00725

XCPShowView("mapview", mapView)
Mustafa
  • 20,504
  • 42
  • 146
  • 209
  • but map tiles never load – ReDetection Feb 17 '15 at 03:34
  • It did load the map for me once, but now after waiting for a while I get `GEOResourceManifestServerRemoteProxy: Lost connection to geod (Connection invalid)` and `Lost connection too many times consecutively. Will try again in 30 seconds... (Is the com.apple.geod.plist correct?)`. This _might_ be a bug in Playground. – Mustafa Feb 17 '15 at 06:44
  • In Xcode 6.2 this just stalls my Playground and sometimes it gives an error message stating that I need to restart the playground as Xcode has lost connection. – holroy Mar 14 '15 at 13:50
2

I try this in Swift Playground App, I just some few modifications and add one line of your code. My swift playground app is using Swift 3.1. I wish I could upload a video, in this mapview I can zoom in and out plus I can move around in the map. https://i.stack.imgur.com/XVqPg.jpg

import UIKit
import PlaygroundSupport
import MapKit 


var myView:UIView = UIView()
myView.frame = CGRect(origin: CGPoint (), size: CGSize(width: 320, height: 568))


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

myview.addSubview(mapView)


PlaygroundPage.current.liveView = mapView
PlaygroundPage.current.needsIndefiniteExecution = true
Isau
  • 21
  • 1