在Swift游乐场中使用MapKit显示地图以便快速查看?

时间:2023-01-23 18:06:51

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

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

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.

我一直在修补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;

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.

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

3 个解决方案

#1


8  

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

对于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 update:

Xcode 9.x,Swift 4.1更新:

import PlaygroundSupport import MapKit

导入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

#2


2  

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)

#3


1  

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

我在Swift Playground App中尝试这个,我只做了一些修改并添加了一行代码。我的swift playground应用程序正在使用Swift 3.1。我希望我可以上传一个视频,在这个mapview中我可以放大和缩小加上我可以在地图中移动。 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

#1


8  

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

对于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 update:

Xcode 9.x,Swift 4.1更新:

import PlaygroundSupport import MapKit

导入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

#2


2  

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)

#3


1  

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

我在Swift Playground App中尝试这个,我只做了一些修改并添加了一行代码。我的swift playground应用程序正在使用Swift 3.1。我希望我可以上传一个视频,在这个mapview中我可以放大和缩小加上我可以在地图中移动。 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