[Xcode10 实际操作]四、常用控件-(15)MKMapView加载简单视图

本文将演示地图视图的使用方法。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

 1 import UIKit
 2 //首先往视图控制器类文件中,引入地图框架
 3 import MapKit
 4 
 5 //添加地理地图代理协议MKMapViewDelegate
 6 class ViewController: UIViewController, MKMapViewDelegate {
 7 
 8     override func viewDidLoad() {
 9         super.viewDidLoad()
10         // Do any additional setup after loading the view, typically from a nib.
11         //初始化一个地图对象,并指定其位置和尺寸
12         let map = MKMapView(frame: self.view.bounds)
13         //在地图中,显示当前用户的地理位置
14         map.showsUserLocation = true
15         //设置地图视图的显示样式,为卫星视图模式
16         map.mapType = MKMapType.satellite
17         
18         //将地图视图对象,添加到当前视图控制器的根视图
19         self.view.addSubview(map)
20     }
21 
22     override func didReceiveMemoryWarning() {
23         super.didReceiveMemoryWarning()
24         // Dispose of any resources that can be recreated.
25     }
26 }

猜你喜欢

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