iOS:Swift關于地圖: 繪制標記點奶赠,繪制線段,繪制圓形药有,繪制多邊形

這里主要說iOS原生地圖
iOS原生地圖很有意思毅戈,在國內是用的高德地圖,在國外才是蘋果地圖~
就比如在國內你在手機上看國外地圖愤惰,非常不詳細苇经,和國內街道、景觀的詳盡程度不可同日而語

一宦言、MapKit里的MKMapView的一些主要屬性和方法

/** 是否可以旋轉 */
var isRotateEnabled: Bool { get set}

/** 是否可以捏和 */
var isPitchEnabled: Bool { get set}

/** 是否顯示指南針 */
var showsCompass: Bool { get set}

/** 顯示定位 */
var showsUserLocation: Bool { get set}

/** 地圖類型 */
var mapType: MKMapType { get set}

/** 顯示區(qū)域 */
var region: MKCoordinateRegion { get set}

/** 中心點 */
var centerCoordinate: CLLocationCoordinate2D { get}

/** 設置顯示區(qū)域 */
func setRegion(_ region: MKCoordinateRegion, animated: Bool)

/** 坐標轉經緯度 */
func convert(_ point: CGPoint, toCoordinateFrom view: UIView?) -> CLLocationCoordinate2D

/** 經緯度轉坐標 */   
func convert(_ coordinate: CLLocationCoordinate2D, toPointTo view: UIView?) -> CGPoint


代理方法
/** 自定義標記點入口 */
(nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;

/** 自定義曲線入口 */
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer;

/** 地圖顯示區(qū)域改變 */
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool);

二扇单、自定義標記點Annotation

1、自定義class: CustomAnnotation: MKPointAnnotation
可以在里面添加一些自定義屬性奠旺,該類可以理解為標記點的model

class CustomAnnotation: MKPointAnnotation {
    var index: Int = 0
    var annotationID: String?
    var title: String?
    ...
}

同時自定義class: CustomAnnotationView: MKAnnotationView
可以在里面自定義控件蜘澜,該類可以理解為標記點View

class CustomAnnotationView: MKAnnotationView {
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        ///自定義控件入口
    }
    override var annotation: MKAnnotation? {
        didSet {
            ///可以在這里去為自定義的控件填充數據
        }
    }
    ...
}

Annotation的增刪改查API

///增
open func addAnnotation(_ annotation: MKAnnotation)

open func addAnnotations(_ annotations: [MKAnnotation])

///刪
open func removeAnnotation(_ annotation: MKAnnotation)

open func removeAnnotations(_ annotations: [MKAnnotation])

///查
open var annotations: [MKAnnotation] { get }

///修改位置(如果要修改數據,只需自己添加入口即可)
open var coordinate: CLLocationCoordinate2D
然后阻桅,將Annotation和AnnotationView綁定起來
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is CustomAnnotation {
            let identifier = "CustomAnnotationIdentifier"
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? CustomAnnotationView
            if annotationView == nil {
                annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            }
       return annotationView
     }
  ...
}

這樣就完成了一個自定義標記點,因為要自定義兩個類,要去添加Annotation,還要在代理方法里綁定兼都,流程很是繁瑣嫂沉,所以可以抽象封裝一下

封裝后簡單的Annotation只需:

let options = AutelAnnotationOptions(coordinate: coordinate, "Img_map_mark".toImage(), nil, false)
let annotation = mapView.createAnnotation(options, nil)
mapView.addAnnotation(annotation)

*****************************************************************************************************
init(coordinate coord: CLLocationCoordinate2D, _ defaultImage: UIImage? = nil, _ selectedImage: UIImage? = nil, _ gestureEnabled: Bool = false)
func createAnnotation(_ options: AutelAnnotationOptions?, _ customView: AutelCustomAnnotationView?) -> AutelAnnotation

如果項目中地圖占比較重,就非常有封裝的必要性扮碧,具體怎么封裝這里就不再說了

三趟章、繪制線:直線、圓慎王、多邊形蚓土、自定義曲線

1.繪制直線

自定義class,CustomLine: MKPolyline

class CustomLine: MKPolyline {
    var strokeColor = UIColor.red
    var width: CGFloat = 5
}

添加線段到地圖上

let line = CustomLine(coordinates:[coor1,coor2], count: 2)
line.strokeColor = .green
line.width = 10
mapView.addOverlay(line, level: .aboveRoads)

代理方法里設置MKPolylineRenderer

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let line = overlay as? CustomLine {
        let render:MKPolylineRenderer = MKPolylineRenderer(polyline: line)
        render.lineWidth = line.width
        render.strokeColor = line.strokeColor
        return render
    }
    ...
}

這樣就完成了一條線段的繪制

如果想要自定義曲線呢赖淤?比如繪制貝塞爾曲線,曲線上加箭頭等等

只需要自定義CustomLineRenderer: MKPolylineRenderer

重寫其draw方法即可

override func draw(_ mapRect: MKMapRect,
                       zoomScale: MKZoomScale,
                       in context: CGContext) {
    ...
}
2.繪制圓蜀漆、多邊形

自定義class,CustomCircle: MKCircle
class CustomPolygon : MKPolygon

添加到地圖上

let circle = CustomCircle(center: center, radius: radius)
mapView.addOverlay(circle)
let polygon = CustomPolygon.init(coordinates: coordinates, count: coordinates.count)
mapView.addOverlay(polygon)

在代理方法里設置render

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = super.mapView(mapView, rendererFor: overlay)
    if let overlay = overlay as? CustomCircle {
        let render = MKCircleRenderer.init(circle: overlay)
        render.strokeColor = UIColor.red
        render.lineWidth = 1
        return render   
    } else if let overlay = overlay as? CustomPolygon {
        let render: MKPolygonRenderer = MKPolygonRenderer.init(polygon: overlay)
        render.fillColor = UIColor.blue
        return render
    }
}

這樣就完成了圓咱旱、多邊形的繪制

同樣,繪制曲線尤其是自定義曲線也比較繁瑣确丢,需要兩個自定義類,添加曲線代碼吐限,代理方法設置鲜侥,四個地方都要寫...所以,如果地圖在項目中比重比較重诸典,也很有必要抽象封裝一下
let options = AutelCircleOptions(center: coordinate, radius: radius)
options.strokeColor = UIColor.blue
options.lineWidth = 2
let circle = mapView.createCircle(options, custom: nil)
mapView.addOverlay(circle)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末描函,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子狐粱,更是在濱河造成了極大的恐慌舀寓,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肌蜻,死亡現場離奇詭異互墓,居然都是意外死亡,警方通過查閱死者的電腦和手機宋欺,發(fā)現死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進店門轰豆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人齿诞,你說我怎么就攤上這事酸休。” “怎么了祷杈?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵斑司,是天一觀的道長。 經常有香客問我,道長宿刮,這世上最難降的妖魔是什么互站? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮僵缺,結果婚禮上胡桃,老公的妹妹穿的比我還像新娘。我一直安慰自己磕潮,他們只是感情好翠胰,可當我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著自脯,像睡著了一般之景。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上膏潮,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天锻狗,我揣著相機與錄音,去河邊找鬼焕参。 笑死轻纪,一個胖子當著我的面吹牛,可吹牛的內容都是我干的龟糕。 我是一名探鬼主播桐磁,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼悔耘,長吁一口氣:“原來是場噩夢啊……” “哼讲岁!你這毒婦竟也來了?” 一聲冷哼從身側響起衬以,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤缓艳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后看峻,有當地人在樹林里發(fā)現了一具尸體阶淘,經...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年互妓,在試婚紗的時候發(fā)現自己被綠了溪窒。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡冯勉,死狀恐怖澈蚌,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情灼狰,我是刑警寧澤宛瞄,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站交胚,受9級特大地震影響份汗,放射性物質發(fā)生泄漏盈电。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一杯活、第九天 我趴在偏房一處隱蔽的房頂上張望匆帚。 院中可真熱鬧,春花似錦旁钧、人聲如沸卷扮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽晤锹。三九已至,卻和暖如春彤委,著一層夾襖步出監(jiān)牢的瞬間鞭铆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工焦影, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留车遂,地道東北人。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓斯辰,卻偏偏與公主長得像舶担,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子彬呻,可洞房花燭夜當晚...
    茶點故事閱讀 45,446評論 2 359

推薦閱讀更多精彩內容