1. 程式人生 > >[Swift通天遁地]一、超級工具-(10)使用地圖檢視MKMapView的相機功能實現建立三維地圖

[Swift通天遁地]一、超級工具-(10)使用地圖檢視MKMapView的相機功能實現建立三維地圖

本文將演示使用地圖檢視MKMapView的相機功能實現建立三維地圖。

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

 1 import UIKit
 2 //在當前的類檔案中引入所需的類庫
 3 import MapKit
 4 
 5 class ViewController: UIViewController {
 6     
 7     override func viewDidLoad() {
 8         super.viewDidLoad()
 9         // Do any additional setup after loading the view, typically from a nib.
10 11 //初始化一個地圖檢視,並使地圖檢視的顯示區域,和裝置的螢幕尺寸相同 12 let mapView = MKMapView(frame: self.view.bounds) 13 //設定地圖的型別為標準型別 14 mapView.mapType = MKMapType.standard 15 16 //初始化一個地理座標,使地圖載入該座標位置上的地理資訊。 17 let center = CLLocationCoordinate2DMake(39.915352
, 116.397105) 18 //建立另一個地理座標,作為相機的起點座標。 19 let fromEye = CLLocationCoordinate2DMake(39.915352+0.1, 116.397105+0.1) 20 //初始化一個地理距離常量,作為相機的高度。 21 let altitude : CLLocationDistance = 100 22 23 //使用上文的幾個引數,建立一臺地圖相機。 24 //並依次設定相機的引數。 25 //1.目標點座標 26
//2.起點座標 27 //3.高度 28 let camera = MKMapCamera(lookingAtCenter: center,//1.目標點座標 29 fromEyeCoordinate: fromEye, //2.起點座標 30 eyeAltitude: altitude)//3.高度 31 //設定地圖檢視的相機屬性 32 mapView.camera = camera 33 34 //將地圖檢視新增到根檢視中 35 self.view.addSubview(mapView) 36 } 37 38 override func didReceiveMemoryWarning() { 39 super.didReceiveMemoryWarning() 40 // Dispose of any resources that can be recreated. 41 } 42 }