質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1538閲覧

MapKitでボタンを押したときに別のViewへ遷移したいです。(SwiftUIを使用)

Swift_Begginer

総合スコア4

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2022/05/02 06:07

MapKitとSwiftUIを用いて、

①長押しした際にピンを立てる。
②そのピンから吹き出し(UIbutton付き)を出す。
③UIbuttonをタッチした際、別のViewを表示する。

ということをしたく、試しにprintで、適当な文字を表示しようと思い、
下記のようにコードを書いたのですが上手くいきませんでした。
②までのUIbutton付きの吹き出しを出すことはできたのですが、ボタンを押しても何の挙動も得られない状態です。
※できればボタンを複数個作成し、それぞれ別の挙動をさせたいです。

何も起きない原因と、解決方法をご教授いただきたいです。
よろしくお願いいたします。

swift

1 2import SwiftUI 3import MapKit 4 5struct MapView: UIViewRepresentable { 6 7 func makeUIView(context: Context) -> MKMapView { 8 let map = MKMapView() 9 map.delegate = context.coordinator 10 map.addGestureRecognizer(context.coordinator.myLongPress) 11 return map 12 } 13 14 func makeCoordinator() -> Coordinator { 15 Coordinator(self) 16 } 17 18 func updateUIView(_ mapView: MKMapView, context: Context) { 19 let latitude = 43.31513, longitude = 143.08874 20 let coordinate = CLLocationCoordinate2DMake(latitude, longitude) 21 // 縮尺を設定 22 let span = MKCoordinateSpan(latitudeDelta: 5, longitudeDelta: 5) 23 // マップの中心を設定 24 let region = MKCoordinateRegion(center: coordinate, span: span) 25 mapView.setRegion(region, animated: true) 26 } 27 28 class Coordinator: NSObject, MKMapViewDelegate { 29 var parent: MapView 30 let myLongPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer() 31 32 init(_ parent: MapView) { 33 self.parent = parent 34 super.init() 35 self.myLongPress.addTarget(self, action: #selector(recognizeLongPress)) 36 } 37 38 @objc func recognizeLongPress(sender: UILongPressGestureRecognizer) { 39 if sender.state == .ended { 40 if let mapView = sender.view as? MKMapView { 41 // タップした位置を取得 42 let point = sender.location(in: mapView) 43 // mapView上での位置に変換 44 let coordinate = mapView.convert(point, toCoordinateFrom: mapView) 45 // アノテーション作成 46 let annotation = MKPointAnnotation() 47 annotation.coordinate = coordinate 48 annotation.title = "タイトル" 49 mapView.addAnnotation(annotation) 50 } 51 } 52 } 53 54 //↓ボタン(btn)を押しても呼ばれない。 55 func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 56 print("1234567890")//とりあえず表示出来るか試し 57 //その後は他のViewへ遷移もしたい 58 } 59 60 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 61 let identifier = "annotation" 62 if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { 63 annotationView.annotation = annotation 64 return annotationView 65 } else { 66 let annotationView = MKAnnotationView( 67 annotation: annotation, 68 reuseIdentifier: identifier 69 ) 70 71 //stackViewの設定 72 let stackView = UIStackView() 73 stackView.axis = NSLayoutConstraint.Axis.vertical 74 stackView.alignment = UIStackView.Alignment.leading 75 76 //景色の画像の作成 77 let imageView = UIImageView(image: UIImage(named: "Try")) 78 imageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 79 imageView.contentMode = .scaleAspectFit 80 stackView.addArrangedSubview(imageView) 81 82 //ボタンの作成 83 let btn = UIButton(type: .custom) 84 btn.frame = CGRect(x: 0, y: 0, width: 20, height: 20) 85 btn.setImage(UIImage(named: "Hensyu"), for: .normal) 86 stackView.addArrangedSubview(btn) 87 88 annotationView.image = UIImage(named: "Eat") 89 annotationView.canShowCallout = true 90 annotationView.detailCalloutAccessoryView = stackView 91 annotationView.isUserInteractionEnabled = true 92 return annotationView 93 } 94 } 95 } 96} 97

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Swift_Begginer

2022/05/02 10:07

ご回答ありがとうございます。 添付いただいたURLを参考に修正してみましたが、上手くいかず、 もう少し詳しく解説していただけますと幸いです。 理解が浅く、お手数おかけしますがよろしくお願いします。
guest

回答1

0

ベストアンサー

コメントありがとうございます。
(コードを記述したかったので回答欄に書いてみます)
Coordinatorの部分だけ抜粋ですが、「// *****」のコメントをした部分を追加することで、ボタンをクリックした時の処理は実行できると思います。

swift

1 2 class Coordinator: NSObject, MKMapViewDelegate { 3 var parent: MapView 4 let myLongPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer() 5 6 init(_ parent: MapView) { 7 self.parent = parent 8 super.init() 9 self.myLongPress.addTarget(self, action: #selector(recognizeLongPress)) 10 } 11 12 @objc func recognizeLongPress(sender: UILongPressGestureRecognizer) { 13 if sender.state == .ended { 14 if let mapView = sender.view as? MKMapView { 15 // タップした位置を取得 16 let point = sender.location(in: mapView) 17 // mapView上での位置に変換 18 let coordinate = mapView.convert(point, toCoordinateFrom: mapView) 19 // アノテーション作成 20 let annotation = MKPointAnnotation() 21 annotation.coordinate = coordinate 22 annotation.title = "タイトル" 23 mapView.addAnnotation(annotation) 24 } 25 } 26 } 27 28 //↓ボタン(btn)を押しても呼ばれない。 29 func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 30 print("1234567890")//とりあえず表示出来るか試し 31 //その後は他のViewへ遷移もしたい 32 } 33 34 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 35 let identifier = "annotation" 36 if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { 37 annotationView.annotation = annotation 38 return annotationView 39 } else { 40 let annotationView = MKAnnotationView( 41 annotation: annotation, 42 reuseIdentifier: identifier 43 ) 44 45 //stackViewの設定 46 let stackView = UIStackView() 47 stackView.axis = NSLayoutConstraint.Axis.vertical 48 stackView.alignment = UIStackView.Alignment.leading 49 50 //景色の画像の作成 51 let imageView = UIImageView(image: UIImage(named: "Try")) 52 imageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 53 imageView.contentMode = .scaleAspectFit 54 stackView.addArrangedSubview(imageView) 55 56 //ボタンの作成 57 let btn = UIButton(type: .custom) 58 btn.frame = CGRect(x: 0, y: 0, width: 20, height: 20) 59 btn.setImage(UIImage(named: "Hensyu"), for: .normal) 60 btn.addTarget(self, action: #selector(Coordinator.testAction(_:)), for: .touchUpInside); // ***** addTarget 61 stackView.addArrangedSubview(btn) 62 63 annotationView.image = UIImage(named: "Eat") 64 annotationView.canShowCallout = true 65 annotationView.detailCalloutAccessoryView = stackView 66 annotationView.isUserInteractionEnabled = true 67 return annotationView 68 } 69 } 70 // ***** addTarget(testAction)メソッド 71 @objc func testAction(_ sender: UIButton) { 72 print("testAction") 73 } 74 } 75

投稿2022/05/02 10:16

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Swift_Begginer

2022/05/02 12:02

ご丁寧にコードまで記載いただき、ありがとうございます。 意図した通りに動かすことができました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問