- //
- // ViewController.swift
- // 1114-mapkit
- //
- // Created by targetcloud on 2016/11/14.
- // Copyright © 2016年 targetcloud. All rights reserved.
- //
- 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
- }()
- override func viewDidLoad() {
- super.viewDidLoad()
- // 设置地图样式
- // case Standard 标准
- // case Satellite // 卫星
- // case Hybrid // 混合(标准加卫星)
- // @available(iOS 9.0, *)
- // case SatelliteFlyover 3D立体卫星
- // @available(iOS 9.0, *)
- // case HybridFlyover 3D立体混合
- // mapView.mapType = MKMapType.satellite
- mapView.mapType = MKMapType.standard
- // 设置地图的控制项
- // mapView.scrollEnabled = false
- // mapView.rotateEnabled = false
- // mapView.zoomEnabled = false
- // 设置地图的显示项
- // 建筑物
- mapView.showsBuildings = true
- // 指南针
- if #available(iOS 9.0, *) {
- mapView.showsCompass = true
- // 比例尺
- mapView.showsScale = true
- // 交通状况
- mapView.showsTraffic = true
- }
- // poi兴趣点
- mapView.showsPointsOfInterest = true
- // 1. 显示用户位置
- _ = locationM
- mapView.showsUserLocation = true
- // 2. 用户的追踪模式
- // mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
- mapView.delegate = self
- }
- }
- extension ViewController: MKMapViewDelegate {
- // 当地图更新用户位置信息时调用
- // 蓝点: 大头针"视图" 大头针"数据模型"
- func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
- // MKUserLocation: 大头针数据模型
- // location : 者就是大头针的位置信息(经纬度)
- // heading: 设备朝向
- // title: 弹框标题
- // subtitle: 弹框子标题
- // 移动地图的中心,显示在当前用户所在的位置
- // mapView.setCenter(userLocation.coordinate, animated: true)
- // 设置地图显示区域
- let center = (userLocation.coordinate)
- let span = MKCoordinateSpanMake(0.0219952102009202, 0.0160932558432023)
- let region: MKCoordinateRegion = MKCoordinateRegionMake(center, span)
- mapView.setRegion(region, animated: true)
- }
- //区域改变的时候调用
- func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
- print(mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta)
- }
- }
swift地图定位(十一)MapKit初使用
最新推荐文章于 2019-05-22 10:58:00 发布