Swift MapKit的使用

MapKit :用于地图展示,例如大头针,路线、覆盖层展示等(着重界面展示)

使用步骤

  1. 导入主头文件 import MapKit 并在framework里面添加MapKit
  2. MapKit有一个比较重要的UI控件 :MKMapView,专门用于地图显示
import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var mapkitView: MKMapView!
    lazy var locationM : CLLocationManager =
    {
        let location = CLLocationManager()
        
        if #available(iOS 8.0, *)
        {
            location.requestAlwaysAuthorization()
            if #available(iOS 9.0, *)
            {
                location.allowsBackgroundLocationUpdates = true;
                    
            }
        }
        
        return location
            
    }()
    override func viewDidLoad()
    {
        super.viewDidLoad()
        //1.设置地图样式,地图的样式可以手动设置, 在iOS9.0之前有3种, iOS9.0之后增加了2种
        //        case standard 标准
        //        case satellite // 卫星
        //        case hybrid // 混合(标准加卫星)
        
        //        @available(iOS 9.0, *)
        //        case satelliteFlyover 3D立体卫星
        //        @available(iOS 9.0, *)
        //        case hybridFlyover 3D立体混合
        mapkitView.mapType = .hybrid
        
        //2.设置地图的控制项 ,1. 地图的旋转, 缩放, 移动等等操作行为都可以开启或者关闭
        //mapkitView.isScrollEnabled = false //是否滚动
        //mapkitView.isRotateEnabled = false //是否旋转
        //mapkitView.isZoomEnabled = false //是否缩放
      //mapkitView.isPitchEnabled = false; // 是否显示3DVIEW
        //3. 设置地图显示项 ,地图上的指南针, 比例尺, 建筑物, POI点都可以控制是否显示
        //建筑物
        mapkitView.showsBuildings = true
        
        if #available(iOS 9.0, *)
        {
            //指南针
            mapkitView.showsCompass = true
            //比例尺
            mapkitView.showsScale = true
            //交通状况
            mapkitView.showsTraffic = true
        }
        //兴趣地点
        mapkitView.showsPointsOfInterest = true
        
        //4.1 显示用户位子
        _ = locationM
        // 显示一个蓝点, 在地图上面标示用户的位置信息. **注意事项: 如果要显示用户位置, 在iOS8.0之后, 需要主动请求用户授权**
        // 但是, 不会自动放大地图, 并且当用户 位置移动时, 地图不会自动跟着跑
       // mapkitView.showsUserLocation = true
        //4.2 用户的追踪模式
        // 显示一个蓝点, 在地图上面标示用户的位置信息
        // 会自动放大地图, 并且当用户 位置移动时, 地图会自动跟着跑
        // 不灵光
        mapkitView.userTrackingMode = .followWithHeading

    }

}

模拟追踪用户的位子

        mapkitView.delegate = self
//MARK: - MKMapViewDelegate -
extension ViewController : MKMapViewDelegate
{   // 当地图更新用户位置信息时, 调用的方法
    // 蓝点: 大头针"视图"  大头针"数据模型"
    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation)
    {
        //MKUserLocation 大头针数据模型
        // location : 就是大头针的位置信息(经纬度)
        // heading: 设备朝向对象
        // title: 弹框标题
        // subtitle: 弹框子标题
        userLocation.title = "哥哥"
        userLocation.subtitle = "工作中。。。"
         // 移动地图的中心,显示在当前用户所在的位置, 并改变区域
//MKCoordinateSpan 跨度解释:

//latitudeDelta:纬度跨度,因为南北纬各90.0度,所以此值的范围
//是(0.0---180.0);此值表示,整个地图视图宽度,显示多大跨度;
//longitudeDelta:经度跨度,因为东西经各180.0度,所以此值范围是(0.0---360.0):此值表示,整个地图视图高度,显示多大跨度;
0.00805562331540699 0.006232607891206499
 //       mapView.setCenter(userLocation.coordinate, animated: true)
 let center = (userLocation.location?.coordinate)!
        let span = MKCoordinateSpan(latitudeDelta: 0.00805562331540699, longitudeDelta: 0.006232607891206499)
        let region : MKCoordinateRegion = MKCoordinateRegion(center: center, span: span)
        
        mapView.setRegion(region, animated: true)
    }

//当地图区域改变的时候调用,我们可以获取显示的span区域数据
    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool)
    {
        print(mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta)
        
    }
}


大头针的使用

  • 自定义大头针模型
import UIKit
import MapKit

class UserAnnotation: NSObject, MKAnnotation
{
   //确定大头针的位置
   var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)
   
   //弹框标题
   var title: String?
   //子标题
   var subtitle: String?
   
}
  • 在地图上操作大头针,实际上是控制大头针数据模型
  1. 添加大头针就是添加大头针数据模型
  2. 删除大头针就是删除大头针数据模型
 override func touchesBegan(_ touches: Set, with event: UIEvent?)
    {
        //创建一个大头针数据模型
        let userAnnotation : UserAnnotation = UserAnnotation()
        userAnnotation.coordinate = mapkitView.centerCoordinate
        userAnnotation.title = "好"
        userAnnotation.subtitle = "子标题"
        //添加到地图上面
        mapkitView.addAnnotation(userAnnotation)
        
    }
    
    override func touchesMoved(_ touches: Set, with event: UIEvent?)
    {
        //获取所有大头针的模型
        let annotataions = mapkitView.annotations
        
        //2. 移除
        mapkitView.removeAnnotations(annotataions)
        
    }

添加大头针并显示地理信息 用到反地理编码

  override func touchesBegan(_ touches: Set, with event: UIEvent?)
    {
        //1. 根据当前的点的位置 获得在地图上面的坐标
        let point = touches.first?.location(in: mapkitView)
        //经纬度
        let coordinate = mapkitView.convert(point!, toCoordinateFrom: mapkitView)
        
        
        //2.创建一个大头针
        let userAnnotation = : UserAnnotation = UserAnnotation()
          userAnnotation.title = "title"
          userAnnotation.subtitle = "  "
        userAnnotation.coordinate = coordinate
        
        //反地理编码 取得位子信息
        geoCode.reverseGeocodeLocation(CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude))
        { (places, error) in
            if error == nil
            {
                let mark = places?.first
                userAnnotation.title = mark?.locality
                userAnnotation.subtitle = mark?.name
                self.mapkitView.addAnnotation(userAnnotation)

            }
        }

你可能感兴趣的:(Swift MapKit的使用)