這里主要說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)