通过使用MapKit可以将地图嵌入到视图中,MapKit框架除了可以显示地图,还支持在地图上做标记。
1,通过mapType属性,可以设置地图的显示类型
MKMapType.Standard :标准地图
MKMapType.Satellite :卫星地图
MKMapType.Hybrid :混合地图
2,地图显示范围的设置
MKCoordinateSpan对象设置地图范围,其中包含两个成员latitudeDelta和longtitudeDelta,这两个类型为CLLocationDegrees(实际就是double类型)。
一般设置为多少纬度,1纬度约等于111千米(69英里)
3,添加标记
使用MKPointAnnotation对象可以在地图上任意位置添加大头针,同时还可以给这个标记添加标题和描述。
4,下面通过样例来演示
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
|
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对象,设置地图的范围(越小越精确)
var latDelta = 0.05
var longDelta = 0.05
var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake (latDelta, longDelta)
//定义地图区域和中心坐标(
//使用当前位置
//var center:CLLocation = locationManager.location.coordinate
//使用自定义位置
var center: CLLocation = CLLocation (latitude: 32.029171, longitude: 118.788231)
var currentRegion: MKCoordinateRegion = MKCoordinateRegion (center: center.coordinate,
span: currentLocationSpan)
//设置显示区域
self .mainMapView.setRegion(currentRegion, animated: true )
//创建一个大头针对象
var objectAnnotation = MKPointAnnotation ()
//设置大头针的显示位置
objectAnnotation.coordinate = CLLocation (latitude: 32.029171, longitude: 118.788231).coordinate
//设置点击大头针之后显示的标题
objectAnnotation.title = "南京夫子庙"
//设置点击大头针之后显示的描述
objectAnnotation.subtitle = "南京市秦淮区秦淮河北岸中华路"
//添加大头针
self .mainMapView.addAnnotation(objectAnnotation)
}
} |
5,标记样式的修改
默认标记是一个红色的大头针。通过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
|
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 !, viewForAnnotation annotation: MKAnnotation !)
-> MKAnnotationView ! {
if annotation is MKUserLocation {
return nil
}
let reuserId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuserId)
as ? MKPinAnnotationView
if pinView == nil {
//创建一个大头针视图
pinView = MKPinAnnotationView (annotation: annotation, reuseIdentifier: reuserId)
pinView?.canShowCallout = true
pinView?.animatesDrop = true
//设置大头针颜色
pinView?.pinColor = MKPinAnnotationColor . Green
//设置大头针点击注释视图的右侧按钮样式
pinView?.rightCalloutAccessoryView = UIButton .buttonWithType( UIButtonType . DetailDisclosure )
as ! UIButton
} 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
|
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 ) {
println ( "地图缩放级别发送改变时" )
}
func mapView(mapView: MKMapView !, regionDidChangeAnimated animated: Bool ) {
println ( "地图缩放完毕触法" )
}
func mapViewWillStartLoadingMap(mapView: MKMapView !) {
println ( "开始加载地图" )
}
func mapViewDidFinishLoadingMap(mapView: MKMapView !) {
println ( "地图加载结束" )
}
func mapViewDidFailLoadingMap(mapView: MKMapView !, withError error: NSError !) {
println ( "地图加载失败" )
}
func mapViewWillStartRenderingMap(mapView: MKMapView !) {
println ( "开始渲染下载的地图块" )
}
func mapViewDidFinishRenderingMap(mapView: MKMapView !, fullyRendered: Bool ) {
println ( "渲染下载的地图结束时调用" )
}
func mapViewWillStartLocatingUser(mapView: MKMapView !) {
println ( "正在跟踪用户的位置" )
}
func mapViewDidStopLocatingUser(mapView: MKMapView !) {
println ( "停止跟踪用户的位置" )
}
func mapView(mapView: MKMapView !, didUpdateUserLocation userLocation: MKUserLocation !) {
println ( "更新用户的位置" )
}
func mapView(mapView: MKMapView !, didFailToLocateUserWithError error: NSError !) {
println ( "跟踪用户的位置失败" )
}
func mapView(mapView: MKMapView !, didChangeUserTrackingMode mode: MKUserTrackingMode ,
animated: Bool ) {
println ( "改变UserTrackingMode" )
}
func mapView(mapView: MKMapView !, rendererForOverlay overlay: MKOverlay !) -> MKOverlayRenderer ! {
println ( "设置overlay的渲染" )
return nil
}
func mapView(mapView: MKMapView !, didAddOverlayRenderers renderers: [ AnyObject ]!) {
println ( "地图上加了overlayRenderers后调用" )
}
/*** 下面是大头针标注相关 *****/
func mapView(mapView: MKMapView !, didAddAnnotationViews views: [ AnyObject ]!) {
println ( "添加注释视图" )
}
func mapView(mapView: MKMapView !, annotationView view: MKAnnotationView !,
calloutAccessoryControlTapped control: UIControl !) {
println ( "点击注释视图按钮" )
}
func mapView(mapView: MKMapView !, didSelectAnnotationView view: MKAnnotationView !) {
println ( "点击大头针注释视图" )
}
func mapView(mapView: MKMapView !, didDeselectAnnotationView view: MKAnnotationView !) {
println ( "取消点击大头针注释视图" )
}
func mapView(mapView: MKMapView !, annotationView view: MKAnnotationView !,
didChangeDragState newState: MKAnnotationViewDragState ,
fromOldState oldState: MKAnnotationViewDragState ) {
println ( "移动annotation位置时调用" )
}
} |