前言:
前段时间,在一个项目中需求自定义地图。
于是,咱们选择了接入了高德地图。
基于这次自定义地图的实践,总结一些使用上的一些小细节,并方案落地一系列地图相关的文章。
目录如下:
iOS 高德SDK使用实践(一)—— 简介与初始化地图
iOS 高德SDK使用实践(二)—— 自定义大头针AnnotationView
iOS 高德SDK使用实践(三)—— 自定义气泡CalloutView
本篇将介绍怎么Annotation的默许简略用法,以及自定义大头针AnnotationView。
一、什么是AnnotationView?
AnnotationView是高德地图上的标示控件,能够依据经纬度在高德地图上展现的控件。由于默许是大头针款式,所以AnnotationView也被称作为“大头针”。
默许款式:
二、默许AnnotationView的简略使用
首要,AnnotationView是一个高德地图里的UI控件,每一个AnnotationView都会有一个Annotation与之对应。
因而,咱们需求先初始化一个Annotation对象。
func initAnnotationData() {
let pointAnnotation = MAPointAnnotation()
pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: 39.98289153831738, longitude: 116.4903445241268)
pointAnnotation.title = "360大厦"
pointAnnotation.subtitle = "北京朝阳区酒仙桥路6号院电子城"
mapView.addAnnotation(pointAnnotation)
}
官方建议在viewDidAppear办法中调用。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
initAnnotationData()
}
完成<MAMapViewDelegate>中的mapView:viewForAnnotation:回调办法。
与UITableView等相似,annotationView也有复用战略。
/**
* @brief 依据anntation生成对应的View。
* @param mapView 地图View
* @param annotation 指定的标示
* @return 生成的标示View
*/
func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
if annotation.isKind(of: MAPointAnnotation.self) {
let pointReuseIndetifier = "pointReuseIndetifier"
var annotationView: MAPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier) as! MAPinAnnotationView?
if annotationView == nil {
annotationView = MAPinAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier)
}
annotationView!.canShowCallout = true
annotationView!.animatesDrop = true
annotationView!.isDraggable = true
annotationView!.rightCalloutAccessoryView = UIButton(type: UIButton.ButtonType.detailDisclosure)
return annotationView!
}
return nil
}
效果如下图:
三、怎么自定义AnnotationView?
- 第一步:首要,新建一个类,继承与
MAAnnotationView。 咱们给他个名字CustomAnnotationVIew。
像自定义UIView那样,重写它的init办法。
override init!(annotation: MAAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
// ...
self.addSubview(xxxView)
}
依据咱们的需求,自定义这个CustomAnnotationView。
- 第二步:修正
<MAMapViewDelegate>中的mapView:viewForAnnotation:回调办法。
在所有AnnotationView中,有一个特别的AnnotationView。它便是用来展现当时用户定位的AnnotationView。默许是个“小蓝点”。
咱们就以这个小蓝点为例,来自定义这个AnnotationView。
那咱们怎么区分定位小蓝点与其他AnnotationView呢?
每个AnnotationView对应一个Annotation。而区分的标志便是看Annotation的Type。
| 名称 | 类型 |
|---|---|
| 定位点AnnotationView | MAUserLocation |
| 一般AnnotationView | MAPointAnnotation |
同理,假如咱们想自定义一般AnnotationView,就修正MAPointAnnotation下的AnnotationView生成办法。
/**
* @brief 依据anntation生成对应的View。
* @param mapView 地图View
* @param annotation 指定的标示
* @return 生成的标示View
*/
func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
// 改动用户定位的Annotation
if annotation.isKind(of: MAUserLocation.self) {
userAnnotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: "UserLocationId") as CustomAnnotationView
userAnnotationView?.annotation = annotation
userAnnotationView?.headBoaderColor = .red
userAnnotationView?.headImage = UIImage.init(named: "QiShare")!
userAnnotationView?.canShowCallout = false
userAnnotationView?.tag = userAnnotationViewTag
userAnnotationView?.zIndex = 360 // 控制annotationView的层级,zIndex值越大越在上层
return userAnnotationView!
}
// 改动一般标示的AnnotationView
if annotation.isKind(of: MAPointAnnotation.self) {
let pointReuseIndetifier = "pointReuseIndetifier"
var annotationView: MAPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier) as! MAPinAnnotationView?
if annotationView == nil {
annotationView = MAPinAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier)
}
annotationView!.canShowCallout = true
annotationView!.animatesDrop = true
annotationView!.isDraggable = true
annotationView!.rightCalloutAccessoryView = UIButton(type: UIButton.ButtonType.detailDisclosure)
return annotationView!
}
return nil
}
这样咱们就自定义了定位AnnotationView。效果如下图:
最终,更多详细信息,请参阅:高德地图官方文档



