[Swift通天遁地]1、超級工具-(8)地圖視圖MKMapView的經常使用代理方法

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qtefkrsx-gn.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目錄:[Swift]通天遁地Swiftgit

本文將演示地圖視圖MKMapView的經常使用代理方法。github

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】swift

如今開始編寫代碼,建立一個地圖視圖,並設置地圖視圖的代理對象。微信

 1 import UIKit
 2 //在當前的類文件中引入所需的類庫
 3 import MapKit
 4 //給當前的類添加一個地圖視圖的代理協議MKMapViewDelegate
 5 class ViewController: UIViewController, MKMapViewDelegate {
 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.delegate = self
15         //設置地圖的類型爲標準類型
16         mapView.mapType = MKMapType.standard
17         
18         //初始化一個地理座標,使地圖加載該座標位置上的地理信息
19         let coordinate2D = CLLocationCoordinate2D(latitude: 39.915352, longitude: 116.397105)
20         //根據地理座標,初始化一個地理區域,並設置縮放比例
21         let region = MKCoordinateRegionMake(coordinate2D, MKCoordinateSpanMake(0.02, 0.02))
22         //設置地圖的顯示區域
23         mapView.setRegion(region, animated: true)
24         
25         //將地圖視圖添加到當前視圖控制器的根視圖
26         self.view.addSubview(mapView)
27     }
28     
29     //1.添加一個代理方法,用來監聽地圖區域將要發生變化時的動做。
30     //當用戶在地圖上進行平移或縮放操做時,該方法會被調用。
31     func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
32         print("regionWillChange")
33     }
34     
35     //2.添加一個代理方法,用來監聽地圖區域完成變化時的動做。
36     func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
37         print("regionDidChange")
38     }
39     
40     //3.添加一個代理方法,當地圖視圖即將加載地圖信息時,調用此方法。
41     func mapViewWillStartLoadingMap(_ mapView: MKMapView) {
42         print("mapViewWillStartLoadingMap")
43     }
44 
45     //4.添加一個代理方法,當地圖視圖完成地圖信息的加載時,調用此方法。
46     func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
47         print("mapViewDidFinishLoadingMap")
48     }
49     
50     //5.添加一個代理方法,當地圖視圖即將渲染地圖信息時,調用此方法。
51     func mapViewWillStartRenderingMap(_ mapView: MKMapView) {
52         print("mapViewWillStartRenderingMap")
53     }
54     
55     //6.添加一個代理方法,當地圖視圖完成地圖信息的渲染時,調用此方法。
56     func mapViewDidFinishRenderingMap(_ mapView: MKMapView, fullyRendered: Bool) {
57         print("mapViewDidFinishRenderingMap")
58     }
59 
60     override func didReceiveMemoryWarning() {
61         super.didReceiveMemoryWarning()
62         // Dispose of any resources that can be recreated.
63     }
64 }
相關文章
相關標籤/搜索