Swiftでいこう。 -- 地図を表示
Playgroundsで地図を表示してみまよう。
まず地図を表示するときのおまじない。
import MapKit
import CoreLocation
これに基本形を重ねて
import UIKit
import MapKit
import CoreLocation
import PlaygroundSupport
var view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 400))
view.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
PlaygroundPage.current.liveView = view
これで準備はOK!
地図を表示するメソッドを付け加えます。
var mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 300, height: 400))
view.addSubview(mapView)
あとは場所を緯度、経度に合わせてそこにセンターを合わせて表示させます。
var locationManager = CLLocationManager()
let location = CLLocationCoordinate2DMake(34.83874724990397,134.6925609854984)
mapView.setCenter(location,animated:true)
var region:MKCoordinateRegion = mapView.region
region.center = location
region.span.latitudeDelta = 0.01
region.span.longitudeDelta = 0.01
mapView.setRegion(region,animated:true)
mapView.mapType = MKMapType.mutedStandard
実行すると、
表示されます。
この記事が気に入ったらサポートをしてみませんか?