去我的博客查看:https://izumi.pub/iOS/596
1.純圖片替換
// 只需在以下函數(shù)中重設(shè)annotationView圖片就行
func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
if annotation.isKind(of: MAPointAnnotation.self) {
if annotation.title != "當(dāng)前位置" {
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!.image = UIImage(named: "公交站牌32")
// 圖片偏移量贷屎,根據(jù)自己圖片不同來設(shè)定
annotationView!.centerOffset = CGPoint(x: 0, y: -18);
return annotationView!
}
}
return nil
}
點(diǎn)擊以后彈出
但我們?nèi)绻窍胪瑫r顯示圖標(biāo)和文字皱蹦,則需要自己自定義標(biāo)記
2.自定義樣式同時顯示圖片文字
1.自定義一個UIView組件
class CustomStationAnnotationView: UIView {
@IBOutlet weak var stationImage: UIImageView!
@IBOutlet weak var stationLabel: UILabel!
//布局相關(guān)設(shè)置
override func layoutSubviews() {
super.layoutSubviews()
}
/*** 下面的幾個方法都是為了讓這個自定義類能將xib里的view加載進(jìn)來舅桩。這個是通用的,我們不需修改姚垃。 ****/
var contentView:UIView!
//初始化時將xib中的view添加進(jìn)來
override init(frame: CGRect) {
super.init(frame: frame)
contentView = loadViewFromNib()
// 設(shè)置字體白对、背景顏色
stationLabel.backgroundColor = ViewUtility.UIColorFromRGB(color_vaule: "#485A73")
stationLabel.textColor = ViewUtility.UIColorFromRGB(color_vaule: "#EEDA1B")
addSubview(contentView)
}
//初始化時將xib中的view添加進(jìn)來
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
contentView = loadViewFromNib()
addSubview(contentView)
}
//加載xib
func loadViewFromNib() -> UIView {
let className = type(of: self)
let bundle = Bundle(for: className)
let name = NSStringFromClass(className).components(separatedBy: ".").last
let nib = UINib(nibName: name!, bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
}
站點(diǎn)樣式暫時就畫成下面這個樣子层释,注意將Background
設(shè)為Clear Color
昔脯,即無背景色
2.自定義CustomAnnotationView
新建一個類CustomAnnotationView
,繼承MAAnnotationView
放刨,重寫init
引入自定義的UIView
import UIKit
class CustomAnnotationView: MAAnnotationView {
override init!(annotation: MAAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
// 引入自定義UIView
let station = CustomStationAnnotationView.init(frame: CGRect.init(x: 0, y: 0, width: 80, height: 60))
// 設(shè)置樣式里的站點(diǎn)名稱
let title:String = annotation.title as! String
station.stationLabel.text = title
self.addSubview(station)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
3.添加站點(diǎn)信息
首先創(chuàng)建一個mapview
(這部分不作講解)工秩,然后添加自定義點(diǎn)標(biāo)記
// Station為一個自定義的對象,包含 名稱經(jīng)緯度 就行
var stationArray=Array<Station>()
var showStationAnnotationArray=Array<MAPointAnnotation>()
// 本地添加一個站點(diǎn)數(shù)據(jù)
func addStationArray() {
let station = Station.init()
station.name = "公交站"
station.lat = 2253242
station.lng = 11395239
stationArray.append(station)
self.showStationPoint()
}
// MARK: - 添加站點(diǎn)信息
func showStationPoint() {
for station in stationArray {
let pointAnnotation = MAPointAnnotation()
pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: Double(station.lat)/100000.0, longitude: Double(station.lng)/100000.0)
pointAnnotation.title = station.name
pointAnnotation.subtitle = station.note
showStationAnnotationArray.append(pointAnnotation)
}
mapview.addAnnotations(showStationAnnotationArray)
}
//MARK: - 設(shè)置地圖站點(diǎn)樣式
func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
if annotation.isKind(of: MAPointAnnotation.self) {
if annotation.title != "當(dāng)前位置" {
let customReuseIndetifier: String = "customReuseIndetifier"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: customReuseIndetifier) as? CustomAnnotationView
if annotationView == nil {
annotationView = CustomAnnotationView.init(annotation: annotation, reuseIdentifier: customReuseIndetifier)
}
annotationView?.canShowCallout = false
annotationView?.isDraggable = true
annotationView!.centerOffset = CGPoint(x: -40, y: -30)
return annotationView!
}
}
return nil
}
最終展示效果如圖: