回答編集履歴
1
MapViewクラスのコードを追記しました。
    
        answer	
    CHANGED
    
    | @@ -13,3 +13,122 @@ | |
| 13 13 |  | 
| 14 14 | 
             
            @Bindingなどについて、面倒かもしれませんが、公式のチュートリアルの内容を一から実施してみて理解しておくと良いかもしれません。
         | 
| 15 15 | 
             
            https://developer.apple.com/tutorials/swiftui/creating-and-combining-views
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            ## 追記です
         | 
| 18 | 
            +
             | 
| 19 | 
            +
             | 
| 20 | 
            +
            コメントありがとうございます。
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            MapViewクラスだけ修正イメージを貼り付けてみます。
         | 
| 23 | 
            +
            (動かしていないので雰囲気だけ見てください。。)
         | 
| 24 | 
            +
            (`// *****`の付近が主な修正です。)
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            ```swift
         | 
| 27 | 
            +
            struct MapView: UIViewRepresentable {
         | 
| 28 | 
            +
                
         | 
| 29 | 
            +
                // ***** @Binding
         | 
| 30 | 
            +
                @Binding var num:Int
         | 
| 31 | 
            +
                @Binding var titlename:String
         | 
| 32 | 
            +
                @Binding var image: UIImage?
         | 
| 33 | 
            +
                
         | 
| 34 | 
            +
                func makeUIView(context: Context) -> MKMapView {
         | 
| 35 | 
            +
                    let map = MKMapView()
         | 
| 36 | 
            +
                    map.delegate = context.coordinator
         | 
| 37 | 
            +
                    map.addGestureRecognizer(context.coordinator.myLongPress)
         | 
| 38 | 
            +
                    return map
         | 
| 39 | 
            +
                }
         | 
| 40 | 
            +
                func makeCoordinator() -> Coordinator {
         | 
| 41 | 
            +
                    Coordinator(self)
         | 
| 42 | 
            +
                }
         | 
| 43 | 
            +
                func updateUIView(_ mapView: MKMapView, context: Context) {
         | 
| 44 | 
            +
                    let latitude = 43.31513, longitude = 143.08874
         | 
| 45 | 
            +
                    let coordinate = CLLocationCoordinate2DMake(latitude, longitude)
         | 
| 46 | 
            +
                    // 縮尺を設定
         | 
| 47 | 
            +
                    let span = MKCoordinateSpan(latitudeDelta: 5, longitudeDelta: 5)
         | 
| 48 | 
            +
                    // マップの中心を設定
         | 
| 49 | 
            +
                    let region = MKCoordinateRegion(center: coordinate, span: span)
         | 
| 50 | 
            +
                    mapView.setRegion(region, animated: true)
         | 
| 51 | 
            +
                }
         | 
| 52 | 
            +
                
         | 
| 53 | 
            +
                
         | 
| 54 | 
            +
                class Coordinator: NSObject, MKMapViewDelegate {
         | 
| 55 | 
            +
                    
         | 
| 56 | 
            +
                    var parent: MapView
         | 
| 57 | 
            +
                    let myLongPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer()
         | 
| 58 | 
            +
                    
         | 
| 59 | 
            +
                    init(_ parent: MapView) {
         | 
| 60 | 
            +
                        self.parent = parent
         | 
| 61 | 
            +
                        super.init()
         | 
| 62 | 
            +
                        self.myLongPress.addTarget(self, action: #selector(recognizeLongPress))
         | 
| 63 | 
            +
                    }
         | 
| 64 | 
            +
                    
         | 
| 65 | 
            +
                    @objc func recognizeLongPress(sender: UILongPressGestureRecognizer) {
         | 
| 66 | 
            +
                        if sender.state == .ended {
         | 
| 67 | 
            +
                            if let mapView = sender.view as? MKMapView {
         | 
| 68 | 
            +
                                // タップした位置を取得
         | 
| 69 | 
            +
                                let point = sender.location(in: mapView)
         | 
| 70 | 
            +
                                // mapView上での位置に変換
         | 
| 71 | 
            +
                                let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
         | 
| 72 | 
            +
                                print(coordinate.latitude)
         | 
| 73 | 
            +
                                print(coordinate.longitude)
         | 
| 74 | 
            +
                                print("-")
         | 
| 75 | 
            +
                                // アノテーション作成
         | 
| 76 | 
            +
                                let annotation = MKPointAnnotation()
         | 
| 77 | 
            +
                                annotation.coordinate = coordinate
         | 
| 78 | 
            +
                                //annotation.title = "taitooooru"
         | 
| 79 | 
            +
                                mapView.addAnnotation(annotation)
         | 
| 80 | 
            +
                            }
         | 
| 81 | 
            +
                        }
         | 
| 82 | 
            +
                    }
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
         | 
| 85 | 
            +
                        
         | 
| 86 | 
            +
                        //@State var num:Int = 1
         | 
| 87 | 
            +
                        //@State var titlename:String = "ssss"
         | 
| 88 | 
            +
                        
         | 
| 89 | 
            +
                        
         | 
| 90 | 
            +
                        let identifier = "annotation"
         | 
| 91 | 
            +
                        if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
         | 
| 92 | 
            +
                            annotationView.annotation = annotation
         | 
| 93 | 
            +
                            return annotationView
         | 
| 94 | 
            +
                        } else {
         | 
| 95 | 
            +
                            let annotationView = MKAnnotationView(
         | 
| 96 | 
            +
                                annotation: annotation,
         | 
| 97 | 
            +
                                reuseIdentifier: identifier
         | 
| 98 | 
            +
                            )
         | 
| 99 | 
            +
                            
         | 
| 100 | 
            +
                            // ***** MapViewクラスの@Bindingのプロパティを参照します
         | 
| 101 | 
            +
                            //ピンのスタイルを変更
         | 
| 102 | 
            +
                            if parent.num == 1{
         | 
| 103 | 
            +
                                annotationView.image = UIImage(named: "pre")
         | 
| 104 | 
            +
                            }else if parent.num == 2{
         | 
| 105 | 
            +
                                annotationView.image = UIImage(named: "Eat")
         | 
| 106 | 
            +
                            }
         | 
| 107 | 
            +
                            
         | 
| 108 | 
            +
                            //stackViewの設定
         | 
| 109 | 
            +
                            let stackView = UIStackView()
         | 
| 110 | 
            +
                            stackView.axis = NSLayoutConstraint.Axis.vertical
         | 
| 111 | 
            +
                            stackView.alignment = UIStackView.Alignment.leading
         | 
| 112 | 
            +
                            
         | 
| 113 | 
            +
                            //ラベルの作成
         | 
| 114 | 
            +
                            let title = UILabel()
         | 
| 115 | 
            +
                            title.text = "\(parent.titlename)"
         | 
| 116 | 
            +
                            title.font = UIFont.boldSystemFont(ofSize: 20)
         | 
| 117 | 
            +
                            title.isUserInteractionEnabled = true
         | 
| 118 | 
            +
                            stackView.addArrangedSubview(title)
         | 
| 119 | 
            +
                            
         | 
| 120 | 
            +
                            //景色の画像の作成
         | 
| 121 | 
            +
                            let imageView = UIImageView(image: UIImage(named: "Try"))
         | 
| 122 | 
            +
                            imageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
         | 
| 123 | 
            +
                            imageView.contentMode = .scaleAspectFit
         | 
| 124 | 
            +
                            stackView.addArrangedSubview(imageView)
         | 
| 125 | 
            +
                            
         | 
| 126 | 
            +
                            annotationView.canShowCallout = true
         | 
| 127 | 
            +
                            annotationView.detailCalloutAccessoryView = stackView
         | 
| 128 | 
            +
                            annotationView.isUserInteractionEnabled = true
         | 
| 129 | 
            +
                            return annotationView
         | 
| 130 | 
            +
                        }
         | 
| 131 | 
            +
                    }
         | 
| 132 | 
            +
                }
         | 
| 133 | 
            +
            }
         | 
| 134 | 
            +
            ```
         | 
