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

Posted

技术标签:

【中文标题】在 Swift 游乐场中使用 MapKit 显示地图以快速查看?【英文标题】:Displaying a Map with MapKit in a Swift playground for a Quick Look? 【发布时间】:2014-06-09 06:32:27 【问题描述】:

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

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

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 功能,而不是仅仅在其中编写 .swift 文件和环境Xcode 并变得更加正式。

【问题讨论】:

***.com/questions/24108093/… 建议游乐场不是交互式的。 也许本教程可以帮助您解决问题:appshocker.com/… 实际上,当添加一行仅包含 mapView 的行时,它确实在错误控制台中显示了错误,但我无法为它们搜索解决方案。我也试过 XCPShowView("Map", mapView) 没有运气 你解决过这个问题吗? 【参考方案1】:

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

【讨论】:

这似乎几乎工作,我得到了地图,网格,它放大到一个点,但它只是加载一个棕褐色的正方形(好像我在一个沙漠)。将地图类型更改为卫星或混合也不会改变任何内容。 @Ziewvater 感谢您的编辑!我想知道为什么它对我来说还不错【参考方案2】:

试试这个:

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)

【讨论】:

但地图图块从不加载 它确实为我加载了一次地图,但现在等待一段时间后,我得到了GEOResourceManifestServerRemoteProxy: Lost connection to geod (Connection invalid)Lost connection too many times consecutively. Will try again in 30 seconds... (Is the com.apple.geod.plist correct?)。这可能是 Playground 中的一个错误。 在 Xcode 6.2 中,这只会让我的 Playground 停止工作,有时它会给出一条错误消息,指出我需要重新启动 Playground,因为 Xcode 失去了连接。【参考方案3】:

我在 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

【讨论】:

以上是关于在 Swift 游乐场中使用 MapKit 显示地图以快速查看?的主要内容,如果未能解决你的问题,请参考以下文章

UItableview行未在swift4中显示mapkit

Swift MapKit - MapView 中不显示注释

使用 mapKit 的地图显示相反的方向(iOS Swift)

Swift 3 Firebase 到 MapKit

如何更新显示的 ETA 和距离值? Swift 4 MKDirections MapKit

如何使用 Mapkit (Swift) 去用户位置