★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-gookpoiw-gt.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目錄:[Swift]通天遁地Swiftgit
本文將演示在地圖視圖MKMapView中添加支持交互動做的標註圖標。github
在【Assets.xcassets】中導入一張圖片【Annotation】,做爲自定義的標註圖標。swift
在項目導航區,打開視圖控制器的代碼文件【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 }