21
11

More than 3 years have passed since last update.

【Swift】MapKit(基礎)

Last updated at Posted at 2021-03-15

フレームワークのインポート

   import MapKit

MKMapViewの生成

   //mapViewの生成
   let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
   self.view.addSubview(mapView)

MKMapViewの中心にピンを生成する

   let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
   self.view.addSubview(mapView)
   //緯度と軽度
   let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(37, 140)
   var region: MKCoordinateRegion = mapView.region
   //表示するマップの中心座標を設定
   region.center = location
   //地図の拡大状態の設定
   //設定しないと地図の表示が最縮小される
   region.span.latitudeDelta = 0.5
   region.span.longitudeDelta = 0.5
   //regionに設定したマップの表示設定をmapViewに反映
   mapView.setRegion(region, animated: true)
   //マップのタイプ
   mapView.mapType = .hybridFlyover
   //マップのデリゲートの設定
   mapView.delegate = self
   //3Dの見え方の設定
   mapView.isPitchEnabled = true
   //ピン留めの生成
   let annotation = MKPointAnnotation()
   //どこにピンを設置するか
   annotation.coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude)
   annotation.title = "タイトル"
   annotation.subtitle = "サブタイトル"
   mapView.addAnnotation(annotation)

複数箇所のピン留め

   mapView.addAnnotations([annotation, annotation2, ...])

MKMapViewDelegate

   extension ViewController: MKMapViewDelegate {
       func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
           print("ピン留めが選択された時に呼ばれる")
       }

       func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
           print("ピン留めが選択解除になった時に呼ばれる")
       }

       func mapViewWillStartLoadingMap(_ mapView: MKMapView) {
           print("マップビューを読み込もうとしている時に呼ばれる")
       }

       func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
           print("マップビューが必要なマップデータを読み込んだ時に呼ばれる")
       }

       func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
           print("ユーザーの場所が更新された時に呼ばれる")
       }

       func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
           print("ひとつ以上のピン留めがマップに追加された時に呼ばれる")
       }
   }

MKMapViewのプロパティ

プロパティ名 説明
delegate MKMapViewDelegate デリゲートを指定
mapType MKMapType .standard: 標準の地図
.satellite: 航空写真
.hybrid: 標準の地図+航空写真
.satelliteFlyover: 3D航空写真
.hybridFlyover: 3D標準の地図+航空写真
isZoomEnabled Bool ズームできるかどうか
isScrollEnabled Bool 画面をスクロールできるかどうか
isRotateEnabled Bool マップが回転するかどうか
showCompass Bool コンパスを右上に表示するかどうか
showsTraffic Bool 交通情報を表示すかどうか
showsBuilding Bool 建物の名前を表示するかどうか
isUserLocationVisble Bool 現在の位置マップに表示されるかどうか
isPitchEnabled Bool 3Dに対応したmapType
region MKcoodinateRegion マップビューで現在表示されているエリア
camera MKMapCamera マップの外観を決定するために使用するカメラ
annotaions [MKAnnotaion] ピン留め
selectedAnnotations [MKAnnotation] 現在選択されているピン留め

MKMapViewのメソッド

メソッド名 説明
setCenter(MKCoordinateRegion, animated: Bool) regionに設定したマップの設定をMKMapViewに反映
addAnnotation(MKAnnotation) 指定したピン留めの追加
removeAnnotation(MKAnnotation) 指定したピン留めの消去
addAnnotations([MKAnnotation]) 複数のピン留めの追加
removeAnnotations([MKAnnotation]) 複数のピン留めの消去

MKMapViewDelegateメソッド

メソッド名 説明
mapViewWillStartLoadMap(MKMapView) マップビューを読み込もうとしたときに呼ばれる
mapViewDidFinishLoadingMap(MKMapView) マップビューが正常にロードした時に呼ばれる
mapView(MKMapView, didUpdate UserLocation: MKUserLocation) ユーザーの場所が更新された時に呼ばれる
mapView(MKMapView, didSelect view: MKAnnotationView) ピン留めが選択された時に呼ばれる
mapView(MKMapView, didDeselect: MKAnnotationView) ピン留めの一つが選択消去された時に呼ばれる
mapView(MKMapView, didAdd views: [MKAnnotationView]) 新しくピン留めがマップに追加された時に呼ばれる
21
11
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
21
11