LoginSignup
7
9

More than 3 years have passed since last update.

MapViewに軌跡を引く

Last updated at Posted at 2019-06-20

MapViewに軌跡を引く

はじめに

地図上に任意の地点を軌跡として線で結ぶ時に調べた内容をここに残したいと思います。

mapLine.png

サンプルプログラム

Githubにアップしてありますので参考にしてください

MKmapViewに軌跡を引くポイント

Step1.線を結ぶ位置をMKPolinelineでインスタンス化する

CLLocationCoordinate2D型の位置を配列としてMKPolylineでインスタンス化します。

let coordinates = [pos1 , pos2]
let polyLine = MKPolyline(coordinates: coordinates, count: coordinates.count)

Step2.MKMapViewにMKPolinelineインスタンスをaddOverlayする

MKMapViewにaddOverlayします

mapView.addOverlay(polyLine)

Step3.MKMapViewDelegateメソッドで線の色と太さを指定する

addOverlayするとMKMapViewDelegateメソッドが呼ばれます。
delegateメソッド内に線の色と太さを指定します。
注意ですが、前もってmapView.delegate = selfとする必要があります。

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let polyline = overlay as? MKPolyline {
        let polylineRenderer = MKPolylineRenderer(polyline: polyline)
        polylineRenderer.strokeColor = .blue
        polylineRenderer.lineWidth = 2.0
        return polylineRenderer
    }
    return MKOverlayRenderer()
}
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9