iOS 高德SDK——swift自定義地圖點(diǎn)標(biāo)記樣式

去我的博客查看: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
}
單純替換UIImage.jpg

點(diǎn)擊以后彈出

點(diǎn)擊點(diǎn)標(biāo)記.jpg

但我們?nèi)绻窍胪瑫r顯示圖標(biāo)和文字,則需要自己自定義標(biāo)記

2.自定義樣式同時顯示圖片文字

1.自定義一個UIView組件

參考鏈接:Swift - XIB結(jié)合UIView制作自定義組件(xib加載繪制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,即無背景色

站點(diǎn)xib.png

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
}

最終展示效果如圖:

站點(diǎn)地圖.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容