回答編集履歴

3

文章修正

2018/06/02 05:00

投稿

clo.momo
clo.momo

スコア27

test CHANGED
@@ -18,9 +18,9 @@
18
18
 
19
19
 
20
20
 
21
- 0. mapView:calloutAccessoryControlTappedでannotationViewmarkerTintColorを使いたいので型キャスト?を使う。これでannotationView.markerTintColor = .blueと設定することができる
21
+ 0. mapView:calloutAccessoryControlTapped  annotationView に markerTintColor を使いたいので型キャスト?を使う。これで annotationView.markerTintColor = .blue と設定することができる
22
22
 
23
- 0. このままだとマーカーを青にして削除した後にマーカーを追加すると青のマーカーを追加してしまうので、削除した後にannotationView.markerTintColor = .redで赤に戻す
23
+ 0. このままだとマーカーを青にして削除した後にマーカーを追加すると青のマーカーを追加してしまうので、削除した後に annotationView.markerTintColor = .red で赤に戻す
24
24
 
25
25
 
26
26
 

2

コード変更

2018/06/02 05:00

投稿

clo.momo
clo.momo

スコア27

test CHANGED
@@ -18,44 +18,130 @@
18
18
 
19
19
 
20
20
 
21
+ 0. mapView:calloutAccessoryControlTappedでannotationViewでmarkerTintColorを使いたいので型キャスト?を使う。これでannotationView.markerTintColor = .blueと設定することができる
22
+
23
+ 0. このままだとマーカーを青にして削除した後にマーカーを追加すると青のマーカーを追加してしまうので、削除した後にannotationView.markerTintColor = .redで赤に戻す
24
+
21
25
 
22
26
 
23
27
  ```Swift
24
28
 
25
-            //ルアウト表示 ☆★☆★☆★これがないとタップしてもコールアウトが表示されない☆★☆★☆★
29
+ //選択したアノテションにボタン追加する
26
30
 
27
- let centerButton = UIButton()
31
+ func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
28
32
 
29
- centerButton.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
33
+ //ユーザーにアノテーションが適用されるのを防ぐ
30
34
 
31
- centerButton.setTitle("コールアウト", for: UIControlState.normal)
35
+ if annotation is MKUserLocation {
32
36
 
37
+ return nil
38
+
39
+ }
40
+
41
+
42
+
43
+ let pinID = "pin"
44
+
45
+ var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinID) as? MKMarkerAnnotationView
46
+
47
+ if annotationView == nil {
48
+
49
+ annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: pinID)
50
+
51
+ } else {
52
+
53
+ annotationView?.annotation = annotation
54
+
55
+ }
56
+
57
+
58
+
59
+ //マーカー設置の時アニメーションをつける
60
+
61
+ annotationView?.animatesWhenAdded = true
62
+
63
+ //マーカーアノテーションにコールアウトを表示
64
+
65
+ annotationView?.canShowCallout = true
66
+
67
+ //コールアウトを表示 ☆★☆★☆★これがないとタップしてもコールアウトが表示されない☆★☆★☆★
68
+
69
+ let centerButton = UIButton()
70
+
71
+ centerButton.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
72
+
73
+ centerButton.setTitle("バブル", for: UIControlState.normal)
74
+
33
- annotationView.detailCalloutAccessoryView = centerButton
75
+ annotationView?.detailCalloutAccessoryView = centerButton
76
+
77
+
78
+
79
+ //左ボタンをアノテーションビューに追加する。
80
+
81
+ let leftButton = UIButton()
82
+
83
+ leftButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
84
+
85
+ leftButton.setTitle("左", for: UIControlState.normal)
86
+
87
+ leftButton.setTitleColor(UIColor.black, for: UIControlState.normal)
88
+
89
+ annotationView?.leftCalloutAccessoryView = leftButton
90
+
91
+
92
+
93
+
94
+
95
+ //右ボタンをアノテーションビューに追加する。
96
+
97
+ let rightButton = UIButton()
98
+
99
+ rightButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
100
+
101
+ rightButton.setTitle("右", for: UIControlState.normal)
102
+
103
+ rightButton.setTitleColor(UIColor.black, for: UIControlState.normal)
104
+
105
+ annotationView?.rightCalloutAccessoryView = rightButton
106
+
107
+
108
+
109
+ return annotationView
110
+
111
+ }
112
+
113
+
114
+
115
+ //吹き出しアクササリー押下時の呼び出しメソッド
116
+
117
+ func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
118
+
119
+ // !!!!☆★☆★ 型キャストでmarkerTintColorを使えるようにする
120
+
121
+ //if letの範囲をif~else~全体にすることでelseでもmarkerTintColorを使えるようにする
122
+
123
+ if let annotationView = view as? MKMarkerAnnotationView {
124
+
125
+ if(control == view.leftCalloutAccessoryView) {
126
+
127
+
128
+
129
+ annotationView.markerTintColor = .blue
34
130
 
35
131
 
36
132
 
37
- //左ボタンをアノテーションビューに追加する。
133
+ } else {
38
134
 
39
- let leftButton = UIButton()
135
+ mapView.removeAnnotation(view.annotation!)
40
136
 
41
- leftButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
137
+ //マーカーを赤に戻さないと、青にして削除した後に追加したマーカーが青になる
42
138
 
43
- leftButton.setTitle("左", for: UIControlState.normal)
139
+ annotationView.markerTintColor = .red
44
140
 
45
- annotationView.leftCalloutAccessoryView = leftButton
141
+ }
46
142
 
143
+ }
47
144
 
48
-
49
-
50
-
51
- //右ボタンをアノテーションビューに追加する。
52
-
53
- let rightButton = UIButton()
145
+ }
54
-
55
- rightButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
56
-
57
- rightButton.setTitle("右", for: UIControlState.normal)
58
-
59
- annotationView.rightCalloutAccessoryView = rightButton
60
146
 
61
147
  ```

1

markerViewをannotationViewに変更

2018/06/02 04:58

投稿

clo.momo
clo.momo

スコア27

test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  centerButton.setTitle("コールアウト", for: UIControlState.normal)
32
32
 
33
- markerView.detailCalloutAccessoryView = centerButton
33
+ annotationView.detailCalloutAccessoryView = centerButton
34
34
 
35
35
 
36
36
 
@@ -42,7 +42,7 @@
42
42
 
43
43
  leftButton.setTitle("左", for: UIControlState.normal)
44
44
 
45
- markerView.leftCalloutAccessoryView = leftButton
45
+ annotationView.leftCalloutAccessoryView = leftButton
46
46
 
47
47
 
48
48
 
@@ -56,6 +56,6 @@
56
56
 
57
57
  rightButton.setTitle("右", for: UIControlState.normal)
58
58
 
59
- markerView.rightCalloutAccessoryView = rightButton
59
+ annotationView.rightCalloutAccessoryView = rightButton
60
60
 
61
61
  ```