zoukankan      html  css  js  c++  java
  • MapKit 大头针基本使用

    import UIKit

    import MapKit

     

    class ViewController: UIViewController {

     

        @IBOutlet weak var mapView: MKMapView!

        

        

        lazy var locationM: CLLocationManager = {

           

            let locationM = CLLocationManager()

            if #available(iOS 8.0, *) {

                locationM.requestAlwaysAuthorization()

            }

            return locationM

            

        }()

        

        lazy var geoCoder: CLGeocoder = {

           

            return CLGeocoder()

            

        }()

        

        

        override func viewDidLoad() {

            super.viewDidLoad()

            

            _ = locationM

            

            let item = MKUserTrackingBarButtonItem(mapView: mapView)

            navigationItem.leftBarButtonItem = item

            

        }

        

        

        // 理论基础

        // 在地图上操作大头针, 实际上操作的是大头针"数据模型"

        // 删除大头针: 移除大头针数据模型

        // 添加大头针: 添加一个大头针数据模型

        

        

        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)

        {

        

            

            

     

            

            // 1. 获取当前点击的位置, 对应的经纬度信息

            let point = touches.first?.locationInView(mapView)

            

            // 把点 转换成为对应的经纬度坐标

            let coordinate = mapView.convertPoint(point!, toCoordinateFromView: mapView)

            

            

            

            

            

            // 2. 直接调用自定义方法添加

            let annotation = addAnnotation(coordinate, title: "美女", subTitle: "约吗")

            

            // 反地理编码的代码

            let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

            geoCoder.reverseGeocodeLocation(location) { (pls: [CLPlacemark]?, error: NSError?) -> Void in

                if error == nil {

                    let pl = pls?.first

                    print(pl)

                    annotation.title = pl?.locality

                    annotation.subtitle = pl?.name

                }

            }

     

            

        }

        

        

        func addAnnotation(coordinate: CLLocationCoordinate2D, title: String, subTitle: String) -> XMGAnnotation {

            // 1, 创建一个大头针数据模型

            let annotation: XMGAnnotation = XMGAnnotation()

            annotation.coordinate = coordinate

            annotation.title = title

            annotation.subtitle = subTitle

            

           

            

            // 2. 添加大头针数据模型, 到地图上

            mapView.addAnnotation(annotation)

            

            return annotation

            

        }

        

        

        

    }

     

     

    extension ViewController: MKMapViewDelegate {

        

        func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {

            

            // 拿到数据模型

            let annotation = view.annotation

            print("选中了(annotation?.title)")

            

            

        }

        

        func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {

            // 拿到数据模型

            let annotation = view.annotation

            print("取消选中了(annotation?.title)")

        }

        

        

        

        func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {

            

        }

        

        

        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

            

            // 自定义大头针

            // 如果想要自定义大头针, 要不使用 MKAnnotationView, 或者是自己定义的子类

            

            let iden = "item"

            var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(iden)

            if annotationView == nil {

                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: iden)

            }

            

            // 非常重要的步骤

            annotationView?.annotation = annotation

            

            

            // 设置打头针的图片

            annotationView?.image = UIImage(named: "category_5")

            

            

            // 设置大头针中心偏移量

            annotationView?.centerOffset = CGPointMake(0, 0)

            

            // 设置弹框

            annotationView?.canShowCallout = true

            // 设置弹框的偏移量

            annotationView?.calloutOffset = CGPointMake(-10, 10)

            

            

            // 设置弹框的左侧视图

            let imageView = UIImageView(frame: CGRectMake(0, 0, 40, 40))

            let image = UIImage(named: "htl.jpg")

            imageView.image = image

            annotationView?.leftCalloutAccessoryView = imageView

            

            // 设置弹框的右侧视图

            let imageView2 = UIImageView(frame: CGRectMake(0, 0, 40, 40))

            let image2 = UIImage(named: "eason.jpg")

            imageView2.image = image2

            annotationView?.rightCalloutAccessoryView = imageView2

            

            // 设置下部弹框

            if #available(iOS 9.0, *) {

                annotationView?.detailCalloutAccessoryView = UISwitch()

            }

            

            // 设置大头针可以拖动

            annotationView?.draggable = true

            

            

            return annotationView

            

            

        }

        

        

        

        /**

         如果当我们添加一个大头针数据模型, 到地图上, 那么地图就会自动调用一个代理方法, 来查找对应的大头针"视图!!!!"

         

         - parameter mapView:    地图

         - parameter annotation: 大头针"数据模型"

         

         - returns: 大头针"视图"

         // 注意事项: 如果这个方法没有实现, 或者返回Nil, 那么就会使用系统默认的大头针视图来显示

         */

        func mapView2(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

            

            // 系统大头这视图对应的类 MKPinAnnotationView

            // 大头针视图和cell一样, 都有一个"循环利用"机制

            // 1. 从缓存池取出大头针视图

            let iden = "item"

            var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(iden) as? MKPinAnnotationView

            

            if annotationView == nil {

                annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: iden)

            }

            

            // 显示弹框

            annotationView?.canShowCallout = true

            

            

            // 设置大头针颜色

    //        annotationView?.pinColor = .Green

            if #available(iOS 9.0, *) {

                annotationView?.pinTintColor = UIColor.blackColor()

            } else {

                // Fallback on earlier versions

            }

            

            

            // 设置下落动画

            annotationView?.animatesDrop = true

            

            

            print("test")

            return annotationView

            

        }

        

        

        

        

    }

     

     

  • 相关阅读:
    Atitit. C#.net clr 2.0 4.0 4.5新特性 v2 s22 1. CLR内部结构 1 2. CLR 版本发展史 3 3. CLR 2.0新特性 4 4. CLR 4 新特性
    Hbase基本命令 悟寰轩
    mvn常用命令 悟寰轩
    linux添加tomcat服务 悟寰轩
    hadoop基本命令 悟寰轩
    Tomcat启动 悟寰轩
    Eclipse自动部署项目到Tomcat的webapps下的有效方法 悟寰轩
    MySQL改变默认编码为utf8 悟寰轩
    myeclipse关闭自动更新 悟寰轩
    Linux命令大全 悟寰轩
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5401551.html
Copyright © 2011-2022 走看看