一つのPinであれば、以下の様にインスタンス変数として保持しておくのが一番簡単です。
swift
1import UIKit
2import MapKit
3
4class ViewController: UIViewController, MKMapViewDelegate {
5
6 // MapView
7 @IBOutlet weak var myMapView: MKMapView!
8
9 // Pinをインスタンス変数として定義
10 let an = MKPointAnnotation()
11
12 // 経度緯度.
13 let myLan: CLLocationDegrees = 37.331741
14 let myLon: CLLocationDegrees = -122.030333
15
16 var center: CLLocationCoordinate2D!
17
18 override func viewDidLoad() {
19 super.viewDidLoad()
20
21 // 地図の中心の座標.
22 center = CLLocationCoordinate2DMake(myLan, myLon)
23 myMapView.centerCoordinate = center
24
25 // 縮尺を指定.
26 let mySpan: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
27 let myRegion: MKCoordinateRegion = MKCoordinateRegion(center: center, span: mySpan)
28
29 // regionをmapViewに追加.
30 myMapView.region = myRegion
31
32 // Pinを作成
33 an.title = "test"
34 an.coordinate = center
35 myMapView.addAnnotation(an)
36 }
37
38 @IBAction func changeTitle(sender: UIButton) {
39 // ボタン押下時にPinのタイトルを変更
40 an.title = "テスト"
41 }
42}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/03 04:56
2018/05/27 19:44
2018/05/27 23:44