LoginSignup
8
6

More than 3 years have passed since last update.

【Swift】mapviewにピンを立てバルーン表示まで行う

Last updated at Posted at 2020-01-25

mapkitを使う

mapviewでピンを立て、なおかつバルーン表示などもやっていきたいと思ったときのコード
現在地は不使用(緯度と経度で場所を指定)

なお、緯度と経度は下記のgeocodingで場所検索すれば一発!
http://www.geocoding.jp/

mapkit.swift

import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var myMapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

    //中心となる場所の座標オブジェクト作成
      //フィリピンのセブ・マクタン空港

   let coordinate = CLLocationCoordinate2DMake(10.310656, 123.98022)

   //種尺を設定
   let span = MKCoordinateSpanMake(0.2, 0.2)

   //範囲オブジェクトを作成
   let region = MKCoordinateRegionMake(coordinate, span)

   //MapViewに範囲オブジェクトを設定
   myMapView.setRegion(region, animated: true)

   //ピンを生成
   let myPin = MKPointAnnotation()

   //ピンの座標設定
   myPin.coordinate = coordinate

   //タイトル、サブタイトルを設定
   myPin.title = "セブ・マクタン空港"
   myPin.subtitle = "Jollibeeうまい"

   //mapViewにピンを追加
   myMapView.addAnnotation(myPin) 

      }

override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

ここまででとりあえずピンが中心地に立ち
タイトル、サブタイトルまで出るようになった

【備考】
Jollibeeはフィリピンのマクドナルドみたいな感じ

8
6
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
8
6