[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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10146034.html