在Swift游乐场中显示带有MapKit的地图以便快速查看?
很高兴看到swift游乐场的标签-很高兴看到这种情况继续下去 我一直在修补Swift,我想知道MapKit是否可以在操场上使用,这样你就可以使用快速查看功能,这样我就可以迭代和预览我的工作。我还没有弄明白语法,所以我想问问是否有人在操场环境中研究过这个问题。我想我需要一些代码来建立它作为一个视图控制器在Swift游乐场中显示带有MapKit的地图以便快速查看?,swift,mapkit,swift-playground,Swift,Mapkit,Swift Playground,很高兴看到swift游乐场的标签-很高兴看到这种情况继续下去 我一直在修补Swift,我想知道MapKit是否可以在操场上使用,这样你就可以使用快速查看功能,这样我就可以迭代和预览我的工作。我还没有弄明白语法,所以我想问问是否有人在操场环境中研究过这个问题。我想我需要一些代码来建立它作为一个视图控制器 import UIKit import MapKit // Sample UIView code which works great in the playground. //var myVie
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文件和环境并变得更正式。试试这个:
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)
对于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
导入地图套件
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
我在Swift游乐场应用程序中尝试了这一点,我只是做了一些修改并添加了一行代码。我的swift游乐场应用程序正在使用swift 3.1。我希望我能上传一个视频,在这个地图视图中,我可以放大和缩小,还可以在地图上四处移动
建议游乐场不是交互式的。也许本教程可以帮助您解决您的问题:事实上,当添加一行仅使用mapView时,它确实在错误控制台中显示了错误,但我无法为它们搜索解决方案。我也尝试过XCPShowView(“Map”,mapView),但没有LuckId。你解决过这个问题吗?但是Map tiles从来没有加载过。它确实为我加载过一次地图,但现在在等待了一段时间后,我得到了
GEOResourceManifestServerRemoteProxy:与geod的连接丢失(连接无效)
和连续多次丢失连接。将在30秒后重试。。。(com.apple.geod.plist是否正确?
。这可能是游乐场中的一个错误。在Xcode 6.2中,这只是使我的游乐场暂停,有时它会给出一条错误消息,说明我需要重新启动游乐场,因为Xcode已失去连接。这似乎几乎可以正常工作,我得到地图、网格,它会放大到一个点,但随后它只加载一个棕褐色正方形(就像我在沙漠中一样)。将地图类型更改为卫星地图或混合地图也不会改变任何东西。@Nilloc在设置liveView
属性后,我可以通过添加行XCPlaygroundPage.currentPage.NeedsDefiniteExecution=true
来实现这一点。@Ziewvater感谢您的编辑!我想知道为什么前一阵子我觉得还不错
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