1. 程式人生 > >[Xcode10 實際操作]四、常用控制元件-(15)MKMapView載入簡單檢視

[Xcode10 實際操作]四、常用控制元件-(15)MKMapView載入簡單檢視

本文將演示地圖檢視的使用方法。

在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】

 1 import UIKit
 2 //首先往檢視控制器類檔案中,引入地圖框架
 3 import MapKit
 4 
 5 //新增地理地圖代理協議MKMapViewDelegate
 6 class ViewController: UIViewController, MKMapViewDelegate {
 7 
 8     override func viewDidLoad() {
 9         super.viewDidLoad()
10
// Do any additional setup after loading the view, typically from a nib. 11 //初始化一個地圖物件,並指定其位置和尺寸 12 let map = MKMapView(frame: self.view.bounds) 13 //在地圖中,顯示當前使用者的地理位置 14 map.showsUserLocation = true 15 //設定地圖檢視的顯示樣式,為衛星檢視模式 16 map.mapType = MKMapType.satellite
17 18 //將地圖檢視物件,新增到當前檢視控制器的根檢視 19 self.view.addSubview(map) 20 } 21 22 override func didReceiveMemoryWarning() { 23 super.didReceiveMemoryWarning() 24 // Dispose of any resources that can be recreated. 25 } 26 }