回答編集履歴
3
文章修正
answer
CHANGED
@@ -8,8 +8,8 @@
|
|
8
8
|
|
9
9
|
detailを設定しているからコールアウトが表示されるけど、leftを設定していないから左ボタンを押しても反応しない。
|
10
10
|
|
11
|
-
0. mapView:calloutAccessoryControlTappedでannotationView
|
11
|
+
0. mapView:calloutAccessoryControlTapped で annotationView に markerTintColor を使いたいので型キャスト?を使う。これで annotationView.markerTintColor = .blue と設定することができる
|
12
|
-
0. このままだとマーカーを青にして削除した後にマーカーを追加すると青のマーカーを追加してしまうので、削除した後にannotationView.markerTintColor = .redで赤に戻す
|
12
|
+
0. このままだとマーカーを青にして削除した後にマーカーを追加すると青のマーカーを追加してしまうので、削除した後に annotationView.markerTintColor = .red で赤に戻す
|
13
13
|
|
14
14
|
```Swift
|
15
15
|
//選択したアノテーションにボタンを追加する
|
2
コード変更
answer
CHANGED
@@ -8,24 +8,67 @@
|
|
8
8
|
|
9
9
|
detailを設定しているからコールアウトが表示されるけど、leftを設定していないから左ボタンを押しても反応しない。
|
10
10
|
|
11
|
+
0. mapView:calloutAccessoryControlTappedでannotationViewでmarkerTintColorを使いたいので型キャスト?を使う。これでannotationView.markerTintColor = .blueと設定することができる
|
12
|
+
0. このままだとマーカーを青にして削除した後にマーカーを追加すると青のマーカーを追加してしまうので、削除した後にannotationView.markerTintColor = .redで赤に戻す
|
11
13
|
|
12
14
|
```Swift
|
13
|
-
//コールアウトを表示 ☆★☆★☆★これがないとタップしてもコールアウトが表示されない☆★☆★☆★
|
14
|
-
let centerButton = UIButton()
|
15
|
-
centerButton.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
|
16
|
-
centerButton.setTitle("コールアウト", for: UIControlState.normal)
|
17
|
-
annotationView.detailCalloutAccessoryView = centerButton
|
18
|
-
|
19
|
-
|
15
|
+
//選択したアノテーションにボタンを追加する
|
16
|
+
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
|
17
|
+
//ユーザーにアノテーションが適用されるのを防ぐ
|
20
|
-
|
18
|
+
if annotation is MKUserLocation {
|
21
|
-
leftButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
|
22
|
-
leftButton.setTitle("左", for: UIControlState.normal)
|
23
|
-
|
19
|
+
return nil
|
20
|
+
}
|
24
21
|
|
22
|
+
let pinID = "pin"
|
23
|
+
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinID) as? MKMarkerAnnotationView
|
24
|
+
if annotationView == nil {
|
25
|
+
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: pinID)
|
26
|
+
} else {
|
27
|
+
annotationView?.annotation = annotation
|
28
|
+
}
|
29
|
+
|
30
|
+
//マーカー設置の時アニメーションをつける
|
31
|
+
annotationView?.animatesWhenAdded = true
|
32
|
+
//マーカーアノテーションにコールアウトを表示
|
33
|
+
annotationView?.canShowCallout = true
|
34
|
+
//コールアウトを表示 ☆★☆★☆★これがないとタップしてもコールアウトが表示されない☆★☆★☆★
|
35
|
+
let centerButton = UIButton()
|
36
|
+
centerButton.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
|
37
|
+
centerButton.setTitle("バブル", for: UIControlState.normal)
|
38
|
+
annotationView?.detailCalloutAccessoryView = centerButton
|
39
|
+
|
40
|
+
//左ボタンをアノテーションビューに追加する。
|
41
|
+
let leftButton = UIButton()
|
42
|
+
leftButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
|
43
|
+
leftButton.setTitle("左", for: UIControlState.normal)
|
44
|
+
leftButton.setTitleColor(UIColor.black, for: UIControlState.normal)
|
45
|
+
annotationView?.leftCalloutAccessoryView = leftButton
|
46
|
+
|
47
|
+
|
48
|
+
//右ボタンをアノテーションビューに追加する。
|
49
|
+
let rightButton = UIButton()
|
50
|
+
rightButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
|
51
|
+
rightButton.setTitle("右", for: UIControlState.normal)
|
52
|
+
rightButton.setTitleColor(UIColor.black, for: UIControlState.normal)
|
53
|
+
annotationView?.rightCalloutAccessoryView = rightButton
|
54
|
+
|
55
|
+
return annotationView
|
56
|
+
}
|
57
|
+
|
58
|
+
//吹き出しアクササリー押下時の呼び出しメソッド
|
59
|
+
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
|
60
|
+
// !!!!☆★☆★ 型キャストでmarkerTintColorを使えるようにする
|
61
|
+
//if letの範囲をif~else~全体にすることでelseでもmarkerTintColorを使えるようにする
|
62
|
+
if let annotationView = view as? MKMarkerAnnotationView {
|
63
|
+
if(control == view.leftCalloutAccessoryView) {
|
64
|
+
|
65
|
+
annotationView.markerTintColor = .blue
|
25
66
|
|
67
|
+
} else {
|
68
|
+
mapView.removeAnnotation(view.annotation!)
|
26
|
-
|
69
|
+
//マーカーを赤に戻さないと、青にして削除した後に追加したマーカーが青になる
|
27
|
-
let rightButton = UIButton()
|
28
|
-
rightButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
|
29
|
-
rightButton.setTitle("右", for: UIControlState.normal)
|
30
|
-
|
70
|
+
annotationView.markerTintColor = .red
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
31
74
|
```
|
1
markerViewをannotationViewに変更
answer
CHANGED
@@ -14,18 +14,18 @@
|
|
14
14
|
let centerButton = UIButton()
|
15
15
|
centerButton.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
|
16
16
|
centerButton.setTitle("コールアウト", for: UIControlState.normal)
|
17
|
-
|
17
|
+
annotationView.detailCalloutAccessoryView = centerButton
|
18
18
|
|
19
19
|
//左ボタンをアノテーションビューに追加する。
|
20
20
|
let leftButton = UIButton()
|
21
21
|
leftButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
|
22
22
|
leftButton.setTitle("左", for: UIControlState.normal)
|
23
|
-
|
23
|
+
annotationView.leftCalloutAccessoryView = leftButton
|
24
24
|
|
25
25
|
|
26
26
|
//右ボタンをアノテーションビューに追加する。
|
27
27
|
let rightButton = UIButton()
|
28
28
|
rightButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
|
29
29
|
rightButton.setTitle("右", for: UIControlState.normal)
|
30
|
-
|
30
|
+
annotationView.rightCalloutAccessoryView = rightButton
|
31
31
|
```
|