web-dev-qa-db-ja.com

クイックルックのためにSwiftプレイグラウンドでMapKitを使用してマップを表示しますか?

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がそれ自体を初期化するための単純なものが欠けていると思います。Xcode内に.Swiftファイルと環境を書き込んで取得するのではなく、mao機能を実行できるかどうかを理解する必要があります。よりフォーマル。

24
Kokanee

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:

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
8
cjohnson318

これを試して:

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
Mustafa

私はこれをSwift Playground Appで試します。いくつかの変更を加えて、コードを1行追加します。私の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
2
Isau