1. 程式人生 > >[Swift通天遁地]一、超級工具-(9)在地圖檢視MKMapView中新增支援互動動作的標註圖示

[Swift通天遁地]一、超級工具-(9)在地圖檢視MKMapView中新增支援互動動作的標註圖示

本文將演示在地圖檢視MKMapView中新增支援互動動作的標註圖示。

在【Assets.xcassets】中匯入一張圖片【Annotation】,作為自定義的標註圖示。

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

  1 import UIKit
  2 //在當前的類檔案中引入所需的類庫
  3 import MapKit
  4 //給當前的類新增一個地圖檢視的代理協議MKMapViewDelegate
  5 class ViewController: UIViewController, MKMapViewDelegate {
  6
7 //新增一個標註變數,作為當前類的屬性 8 var selectedAnnotion : MKAnnotation! 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view, typically from a nib. 13 14 //初始化一個地圖檢視,並使地圖檢視的顯示區域,和裝置的螢幕尺寸相同 15
let mapView = MKMapView(frame: self.view.bounds) 16 //設定地圖的代理物件為當前的檢視控制器物件 17 mapView.delegate = self 18 //設定地圖的型別為標準型別 19 mapView.mapType = MKMapType.standard 20 21 //初始化一個地理座標,使地圖載入該座標位置上的地理資訊 22 let coordinate2D = CLLocationCoordinate2D(latitude: 39.915352
, longitude: 116.397105) 23 //根據地理座標,初始化一個地理區域,並設定縮放比例 24 let region = MKCoordinateRegionMake(coordinate2D, MKCoordinateSpanMake(0.02, 0.02)) 25 //設定地圖的顯示區域 26 mapView.setRegion(region, animated: true) 27 28 //初始化一個點標註物件 29 let objectAnnotation = MKPointAnnotation() 30 //設定點標註物件地理座標 31 objectAnnotation.coordinate = coordinate2D 32 //設定點標註物件的標題文字 33 objectAnnotation.title = "Imperial Palace" 34 //設定點標註物件的子標題的文字內容 35 objectAnnotation.subtitle = "The world's top five palace" 36 //將標註物件新增到地圖檢視 37 mapView.addAnnotation(objectAnnotation) 38 39 //將地圖檢視新增到當前檢視控制器的根檢視 40 self.view.addSubview(mapView) 41 } 42 43 //新增一個代理方法,用來設定並返回標註檢視 44 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 45 { 46 //標註檢視同表格檢視相似,也是採用相同的複用機制。 47 //在此設定一個識別符號,作為標註檢視的複用標識。 48 let identifier = "annotationView" 49 //從地圖檢視中,獲取一個具有相同識別符號的,並且可被複用的標註檢視。 50 var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 51 52 //如果沒有可被複用的標註檢視, 53 if annotationView == nil 54 { 55 //則初始化一個新的標註檢視 56 annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 57 } 58 59 //初始化一個資訊型別的按鈕控制元件,當用戶點選該按鈕時,將彈出一個警告視窗 60 let button = UIButton(type: UIButtonType.infoDark) 61 //給按鈕控制元件繫結點選事件 62 button.addTarget(self, action: #selector(ViewController.showInfo), for: .touchUpInside) 63 //設定標註檢視左側的附加檢視 64 annotationView?.leftCalloutAccessoryView = button 65 //選擇專案中匯入的圖片檔案,作為標註檢視的標註圖片 66 annotationView?.image = UIImage(named: "Annotation") 67 68 //設定處於焦點狀態的標註檢視 69 self.selectedAnnotion = annotation; 70 //允許標註檢視開啟氣泡,以顯示額外的資訊。 71 annotationView?.canShowCallout = true 72 73 //最後返回設定好的標註檢視 74 return annotationView 75 } 76 77 //新增一個方法,用來響應按鈕的點選事件 78 func showInfo(sender : UIButton) 79 { 80 //初始化一個字串常量,作為彈出視窗的資訊內容。 81 let message = "Imperial Palace, China and the world's most complete preservation, the largest wooden structure of ancient buildings." 82 83 //初始化一個警告彈出視窗,並設定彈出視窗的標題和主題內容。 84 let alertView = UIAlertController(title: self.selectedAnnotion.title!, message: message, preferredStyle: UIAlertControllerStyle.alert) 85 //建立一個預設樣式的按鈕, 86 let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil) 87 //並將按鈕新增到彈出視窗中,當點選該按鈕時,關閉彈出視窗。 88 alertView.addAction(OKAction) 89 90 //最後在當前的檢視控制器中,彈出警告視窗。 91 self.present(alertView, animated: true, completion: nil) 92 } 93 94 //新增一個代理方法,用來監聽標註檢視被新增到地圖檢視中的事件 95 func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) 96 { 97 //遍歷所有被新增到地圖檢視中的標註檢視, 98 for view in views 99 { 100 //並在控制檯輸出其座標資訊 101 print("Did add one MKAnnotationView:"+((view.annotation?.title)!)!) 102 } 103 } 104 105 //新增一個代理方法,用來監聽標註檢視處於選擇狀態時的事件。 106 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 107 { 108 //當選中某個標註檢視時,在控制檯輸出相關的座標、標題、子標題等資訊。 109 print(view.annotation?.coordinate) 110 print(view.annotation?.title) 111 print(view.annotation?.subtitle) 112 } 113 114 override func didReceiveMemoryWarning() { 115 super.didReceiveMemoryWarning() 116 // Dispose of any resources that can be recreated. 117 } 118 }