24

很高兴看到 swift-playground 的标签 - 很高兴看到这种情况继续下去。

我一直在修改 Swift,想知道 MapKit 是否可以在操场上使用,以便您可以使用 Quick Look 功能,这样我就可以迭代并预览我的工作。我还没有弄清楚语法所以想我会问是否有人在游乐场环境中探索过这个。我想我需要一些代码来将其建立为视图控制器。

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;

猜想我只是缺少一些简单的东西来让 mapView 自己初始化等。我喜欢解释的操场区域来修补,只需要弄清楚它是否可以做 mao 功能,而不是仅仅在 Xcode 中编写 .swift 文件和环境并获取更正式。

4

3 回答 3

8

对于 OS X 上的 XCode7.1 和 Swift2.1,我打开了时间线并执行了以下操作:

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 更新:

导入 PlaygroundSupport 导入 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
于 2015-11-13T06:41:14.580 回答
4

尝试这个:

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)
于 2015-02-12T09:49:50.937 回答
2

我在 Swift Playground App 中尝试了这个,我只是做了一些修改并添加了一行代码。我的 Swift Playground 应用程序使用的是 Swift 3.1。我希望我可以上传视频,在这个地图视图中我可以放大和缩小,还可以在地图中移动。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
于 2017-06-23T00:48:16.763 回答