swift地图定位(十一)MapKit初使用

  1. //  
  2. //  ViewController.swift  
  3. //  1114-mapkit  
  4. //  
  5. //  Created by targetcloud on 2016/11/14.  
  6. //  Copyright © 2016年 targetcloud. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10. import MapKit  
  11.   
  12. class ViewController: UIViewController {  
  13.   
  14.     @IBOutlet weak var mapView: MKMapView!  
  15.       
  16.     lazy var locationM: CLLocationManager = {  
  17.         let locationM = CLLocationManager()  
  18.         if #available(iOS 8.0, *) {  
  19.             locationM.requestAlwaysAuthorization()  
  20.         }  
  21.         return locationM  
  22.     }()  
  23.       
  24.     override func viewDidLoad() {  
  25.         super.viewDidLoad()  
  26.         // 设置地图样式  
  27.         //        case Standard 标准  
  28.         //        case Satellite // 卫星  
  29.         //        case Hybrid // 混合(标准加卫星)  
  30.         //        @available(iOS 9.0, *)  
  31.         //        case SatelliteFlyover 3D立体卫星  
  32.         //        @available(iOS 9.0, *)  
  33.         //        case HybridFlyover 3D立体混合  
  34. //          mapView.mapType = MKMapType.satellite  
  35.         mapView.mapType = MKMapType.standard  
  36.           
  37.         // 设置地图的控制项  
  38.         //        mapView.scrollEnabled = false  
  39.         //        mapView.rotateEnabled = false  
  40.         //        mapView.zoomEnabled = false  
  41.           
  42.         // 设置地图的显示项  
  43.         // 建筑物  
  44.         mapView.showsBuildings = true  
  45.         // 指南针  
  46.         if #available(iOS 9.0, *) {  
  47.             mapView.showsCompass = true  
  48.             // 比例尺  
  49.             mapView.showsScale = true  
  50.             // 交通状况  
  51.             mapView.showsTraffic = true  
  52.         }  
  53.         // poi兴趣点  
  54.         mapView.showsPointsOfInterest = true  
  55.           
  56.           
  57.         // 1. 显示用户位置  
  58.         _ = locationM  
  59.         mapView.showsUserLocation = true  
  60.           
  61.         // 2. 用户的追踪模式  
  62. //        mapView.userTrackingMode = MKUserTrackingMode.followWithHeading  
  63.           
  64.         mapView.delegate = self  
  65.     }  
  66. }  
  67.   
  68. extension ViewController: MKMapViewDelegate {  
  69.     // 当地图更新用户位置信息时调用  
  70.     // 蓝点: 大头针"视图"  大头针"数据模型"  
  71.     func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {  
  72.         // MKUserLocation: 大头针数据模型  
  73.         // location : 者就是大头针的位置信息(经纬度)  
  74.         // heading: 设备朝向  
  75.         // title: 弹框标题  
  76.         // subtitle: 弹框子标题  
  77.           
  78.         // 移动地图的中心,显示在当前用户所在的位置  
  79. //        mapView.setCenter(userLocation.coordinate, animated: true)  
  80.           
  81.         // 设置地图显示区域  
  82.         let center = (userLocation.coordinate)  
  83.         let span = MKCoordinateSpanMake(0.02199521020092020.0160932558432023)  
  84.         let region: MKCoordinateRegion = MKCoordinateRegionMake(center, span)  
  85.         mapView.setRegion(region, animatedtrue)  
  86.     }  
  87.       
  88.     //区域改变的时候调用  
  89.     func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {  
  90.         print(mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta)  
  91.     }  
  92.       
  93. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值