swift 使用MapKit显示地图,并在地图上做标记


通过使用  MapKit  可以将地图嵌入到视图中, MapKit  框架除了可以显示地图,还支持在地图上做标记。

1,通过mapType属性,可以设置地图的显示类型
MKMapType.standard :标准地图
MKMapType.satellite :卫星地图
MKMapType.hybrid :混合地图

2,地图显示范围的设置
MKCoordinateSpan 对象设置地图范围,其中包含两个成员  latitudeDelta 和  longtitudeDelta,这两个类型为  CLLocationDegrees(实际就是  double 类型)。
一般设置为多少纬度,1纬度约等于111千米(69英里)

3,添加标记
使用  MKPointAnnotation 对象可以在地图上任意位置添加大头针,同时还可以给这个标记添加标题和描述。

4,下面通过样例来演示
原文:Swift - 使用MapKit显示地图,并在地图上做标记
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import  UIKit
import  MapKit
import  CoreLocation
 
class  ViewController UIViewController  {
     
     var  mainMapView:  MKMapView !
     
     //定位管理器
     let  locationManager: CLLocationManager  CLLocationManager ()
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         //使用代码创建
         self .mainMapView =  MKMapView (frame: self .view.frame)
         self .view.addSubview( self .mainMapView)
         
         //地图类型设置 - 标准地图
         self .mainMapView.mapType =  MKMapType .standard
         
         //创建一个MKCoordinateSpan对象,设置地图的范围(越小越精确)
         let  latDelta = 0.05
         let  longDelta = 0.05
         let  currentLocationSpan: MKCoordinateSpan  MKCoordinateSpanMake (latDelta, longDelta)
         
         //定义地图区域和中心坐标(
         //使用当前位置
         //var center:CLLocation = locationManager.location.coordinate
         //使用自定义位置
         let  center: CLLocation  CLLocation (latitude: 32.029171, longitude: 118.788231)
         let  currentRegion: MKCoordinateRegion  MKCoordinateRegion (center: center.coordinate,
             span: currentLocationSpan)
         
         //设置显示区域
         self .mainMapView.setRegion(currentRegion, animated:  true )
         
         //创建一个大头针对象
         let  objectAnnotation =  MKPointAnnotation ()
         //设置大头针的显示位置
         objectAnnotation.coordinate =  CLLocation (latitude: 32.029171,
             longitude: 118.788231).coordinate
         //设置点击大头针之后显示的标题
         objectAnnotation.title =  "南京夫子庙"
         //设置点击大头针之后显示的描述
         objectAnnotation.subtitle =  "南京市秦淮区秦淮河北岸中华路"
         //添加大头针
         self .mainMapView.addAnnotation(objectAnnotation)
     }
}

5,标记样式的修改
默认标记是一个红色的大头针。通过MKMapViewDelegate代理,我们可以自定义大头针的样式,以及点击注释视图右侧按钮样式等。
原文:Swift - 使用MapKit显示地图,并在地图上做标记
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import  UIKit
import  MapKit
import  CoreLocation
 
class  ViewController UIViewController MKMapViewDelegate  {
     
     var  mainMapView:  MKMapView !
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         //使用代码创建
         self .mainMapView =  MKMapView (frame: self .view.frame)
         self .view.addSubview( self .mainMapView)
         
         self .mainMapView.delegate =  self
         
         //添加大头针等相关代码(这个同前面一样,就不再写了)
         //.......
     }
     
     //自定义大头针样式
     func  mapView(_ mapView:  MKMapView , viewFor annotation:  MKAnnotation )
         ->  MKAnnotationView ? {
         if  annotation  is  MKUserLocation  {
             return  nil
         }
         
         let  reuserId =  "pin"
         var  pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuserId)
             as MKPinAnnotationView
         if  pinView ==  nil  {
             //创建一个大头针视图
             pinView =  MKPinAnnotationView (annotation: annotation, reuseIdentifier: reuserId)
             pinView?.canShowCallout =  true
             pinView?.animatesDrop =  true
             //设置大头针颜色
             pinView?.pinTintColor =  UIColor .green
             //设置大头针点击注释视图的右侧按钮样式
             pinView?.rightCalloutAccessoryView =  UIButton (type: .detailDisclosure)
         } else {
             pinView?.annotation = annotation
         }
         
         return  pinView
     }
}

6,地图代理 - MKMapViewDelegate中所有代理方法
MKMapViewDelegate除了可以设置大头针样式,注释视图点击响应等。还可以在地图相关事件发生时(比如缩放,地图加载,位置跟踪等),触发相应的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import  UIKit
import  MapKit
import  CoreLocation
 
class  ViewController UIViewController MKMapViewDelegate  {
     
     var  mainMapView:  MKMapView !
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         //使用代码创建
         self .mainMapView =  MKMapView (frame: self .view.frame)
         self .view.addSubview( self .mainMapView)
         
         self .mainMapView.delegate =  self
     }
     
     func  mapView(_ mapView:  MKMapView , regionWillChangeAnimated animated:  Bool ) {
         print ( "地图缩放级别发送改变时" )
     }
     
     func  mapView(_ mapView:  MKMapView , regionDidChangeAnimated animated:  Bool ) {
         print ( "地图缩放完毕触法" )
     }
     
     func  mapViewWillStartLoadingMap(_ mapView:  MKMapView ) {
         print ( "开始加载地图" )
     }
     
     func  mapViewDidFinishLoadingMap(_ mapView:  MKMapView ) {
         print ( "地图加载结束" )
     }
     
     func  mapViewDidFailLoadingMap(_ mapView:  MKMapView , withError error:  Error ) {
         print ( "地图加载失败" )
     }
     
     func  mapViewWillStartRenderingMap(_ mapView:  MKMapView ) {
         print ( "开始渲染下载的地图块" )
     }
     
     func  mapViewDidFinishRenderingMap(_ mapView:  MKMapView , fullyRendered:  Bool ) {
         print ( "渲染下载的地图结束时调用" )
     }
     
     func  mapViewWillStartLocatingUser(_ mapView:  MKMapView ) {
         print ( "正在跟踪用户的位置" )
     }
     
     func  mapViewDidStopLocatingUser(_ mapView:  MKMapView ) {
         print ( "停止跟踪用户的位置" )
     }
     
     func  mapView(_ mapView:  MKMapView , didUpdate userLocation:  MKUserLocation ) {
         print ( "更新用户的位置" )
     }
     
     func  mapView(_ mapView:  MKMapView , didFailToLocateUserWithError error:  Error ) {
         print ( "跟踪用户的位置失败" )
     }
     
     func  mapView(_ mapView:  MKMapView , didChange mode:  MKUserTrackingMode ,
                  animated:  Bool ) {
         print ( "改变UserTrackingMode" )
     }
     
     func  mapView(_ mapView:  MKMapView , rendererFor overlay:  MKOverlay )
         ->  MKOverlayRenderer  {
         print ( "设置overlay的渲染" )
         return  MKPolylineRenderer ()
     }
     
     private  func  mapView(mapView:  MKMapView ,
                          didAddOverlayRenderers renderers: [ MKOverlayRenderer ]) {
         print ( "地图上加了overlayRenderers后调用" )
     }
     
     /*** 下面是大头针标注相关 *****/
     func  mapView(_ mapView:  MKMapView , didAdd views: [ MKAnnotationView ]) {
         print ( "添加注释视图" )
     }
     
     func  mapView(_ mapView:  MKMapView , annotationView view:  MKAnnotationView ,
                  calloutAccessoryControlTapped control:  UIControl ) {
         print ( "点击注释视图按钮" )
     }
     
     func  mapView(_ mapView:  MKMapView , didSelect view:  MKAnnotationView ) {
         print ( "点击大头针注释视图" )
     }
     
     func  mapView(_ mapView:  MKMapView , didDeselect view:  MKAnnotationView ) {
         print ( "取消点击大头针注释视图" )
     }
     
     func  mapView(_ mapView:  MKMapView , annotationView view:  MKAnnotationView ,
                  didChange newState:  MKAnnotationViewDragState ,
                  fromOldState oldState:  MKAnnotationViewDragState ) {
         print ( "移动annotation位置时调用" )
     }
}

原文出自: www.hangge.com   转载请保留原文链接: http://www.hangge.com/blog/cache/detail_787.html
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【课程特点】1、231节大容量课程:包含了SwiftUI的大部分知识点,详细讲解SwiftUI的方方面面;2、15个超级精彩的实例:包含美食、理财、健身、教育、电子商务等各行业的App实例;3、创新的教学模式:手把手教您SwiftUI用户界面开发技术,一看就懂,一学就会;4、贴心的操作提示:让您的眼睛始终处于操作的焦点位置,不用再满屏找光标;5、语言简洁精练:瞄准问题的核心所在,减少对思维的干扰,并节省您宝贵的时间;6、视频短小精悍:即方便于您的学习和记忆,也方便日后对功能的检索;7、齐全的学习资料:提供所有课程的源码,在Xcode 11 + iOS 13环境下测试通过; 更好的应用,更少的代码!SwiftUI是苹果主推的下一代用户界面搭建技术,具有声明式语法、实时生成界面预览等特性,可以为苹果手机、苹果平板、苹果电脑、苹果电视、苹果手表五个平台搭建统一的用户界面。SwiftUI是一种创新、简单的iOS开发中的界面布局方案,可以通过Swift语言的强大功能,在所有的Apple平台上快速构建用户界面。 仅使用一组工具和API为任何Apple设备构建用户界面。SwiftUI具有易于阅读和自然编写的声明式Swift语法,可与新的Xcode设计工具无缝协作,使您的代码和设计**同步。自动支持动态类型、暗黑模式、本地化和可访问性,意味着您的**行SwiftUI代码已经是您编写过的非常强大的UI代码了。 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值