2014-06-09 40 views
23

很高興看到swift-playground的標籤 - 愛看到這一點繼續。在Swift遊樂場中用MapKit顯示Map以快速查看地圖?

我一直在修補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到init自身等我愛擺弄周圍的解釋遊樂區,只需要弄清楚它是否能夠做到毛澤東的功能,而不是僅僅寫.swift文件和Xcode中的環境,並獲得更多的正式。

+0

http://stackoverflow.com/questions/24108093/how-to-reference-swift-playground-itself?lq=1表明,操場都沒有互動。 –

+0

也許本教程將幫助您找出問題所在:http://www.appshocker.com/swift-programming-ios-101-part-4-mapkit-app-displaying-locations/ –

+0

實際上,它確實顯示錯誤錯誤控制檯添加一行只是mapView,但我不能爲他們的谷歌解決方案。我也試過XCPShowView(「Map」,mapView)沒有運氣 – ReDetection

回答

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) 
+0

但地圖瓷磚永不加載 – ReDetection

+0

它曾爲我加載地圖一次,但現在等待一段時間後,我得到了'GEOResourceManifestServerRemoteProxy:與地理連接失去連接(連接無效)'和'連續丟失了太多次連接。將在30秒內再次嘗試......(是否com.apple.geod.plist正確?)'。這可能是Playground中的一個錯誤。 – Mustafa

+0

在Xcode 6.2中,這只是拖延我的遊樂場,有時它會給出一個錯誤消息,指出我需要重新啓動操場,因爲Xcode已經失去連接。 – holroy

7

對於XCode7.1和Swift2.1在OS X上,我打開了時間表,並做了以下內容:

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 
+0

這似乎* *工作,我得到地圖,網格,它放大到一個點,但它只是加載一個棕褐色的方塊(就像我在沙漠中)。將地圖類型更改爲Satellite或Hybrid也不會改變任何內容。 – Nilloc

+1

@Nilloc在設置'liveView'屬性後,我可以通過添加'XCPlaygroundPage.currentPage.needsIndefiniteExecution = true'這一行來實現。 – Ziewvater

+0

@Ziewvater感謝編輯!我想知道爲什麼它在一段時間後出現好了 – cjohnson318

1

我試試這個斯威夫特遊樂場應用,我只做了一些修改並添加了一行代碼。我的Swift遊戲應用程序使用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