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

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

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

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

Q&A

0回答

245閲覧

ドラッグでのアノテーションの移動について

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

0クリップ

投稿2018/06/03 06:03

編集2018/06/03 22:21

いつもお世話になります。
MarkerAnnotationViewをドラッグで移動させる際に、Markerが消えてしまいますが、
PinAnnotationViewのときのようにドラッグ中にCalloutを非表示にし、Markerを表示させるようにするにはどうすればいいでしょうか?

MarkerAnnotationView

PinAnnotationView

MarkerAnnotationView without Callout

MarkerAnnotationのコード↓

swift

1// ロングタップ開始時にタップされた座標にアノテーションを追加する 2 @IBAction func mapLongPressed(_ sender: UILongPressGestureRecognizer) { 3 // マップビュー内のタップした位置を取得する。 4 let location: CGPoint = sender.location(in: mapView) 5 // ロングタップ開始時にアノテーションを追加する 6 if (sender.state == UIGestureRecognizerState.began){ 7 // タップした位置を緯度、経度の座標に変換する。 8 let mapPoint:CLLocationCoordinate2D = mapView.convert(location, toCoordinateFrom: mapView) 9 // アノテーションを追加する 10 let annotation = MKPointAnnotation() 11 annotation.coordinate = CLLocationCoordinate2DMake(mapPoint.latitude, mapPoint.longitude) 12 annotation.title = "title" 13 annotation.subtitle = "subtitle" 14 mapView.addAnnotation(annotation) 15 } 16 } 17 18 //アノテーションビューを返すメソッド 19 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 20 // アノテーションビューをマップビューから取得・取得できれば再利用する 21 var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: "Pin") as? MKMarkerAnnotationView 22 23 if (pinView == nil){ 24 // アノテーションビューを生成する 25 pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "Pin") 26 } else { 27 // アノテーションビューに座標・タイトル・サブタイトルを設定する 28 pinView!.annotation = annotation 29 } 30 31 // アノテーションビューに色を設定する 32 pinView.markerTintColor = UIColor.red 33 // 吹き出しの表示をONにする 34 pinView.canShowCallout = true 35 // ドラッグ可能にする 36 pinView.isDraggable = true 37 // ピンのアニメーションをONにする。 38 //pinView.animatesDrop = true 39 // markerのアニメーションをonにする 40 pinView.animatesWhenAdded = true 41 42 //左ボタンをアノテーションビューに追加する。 43 let button = UIButton() 44 button.frame = CGRect(x: 0, y: 0, width: 40, height: 40) 45 button.setTitle("left", for: .normal) 46 button.setTitleColor(UIColor.black, for:.normal) 47 button.backgroundColor = UIColor.yellow 48 pinView.leftCalloutAccessoryView = button 49 50 //右ボタンをアノテーションビューに追加する。 51 let button2 = UIButton() 52 button2.frame = CGRect(x: 0, y: 0, width: 40, height: 40) 53 button2.setTitle("right", for: .normal) 54 button2.backgroundColor = UIColor.red 55 button2.setTitleColor(UIColor.white, for:.normal) 56 pinView.rightCalloutAccessoryView = button2 57 58 return pinView 59 } 60 61 //吹き出しアクササリー押下時の呼び出しメソッド 62 func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 63 if(control == view.leftCalloutAccessoryView) { 64 print("rightButtonTapped") 65 } else { 66 print("leftButtonTapped") 67 } 68 } 69

Pinのコードは下記を変更しているだけです。

swift

1MKMarkerAnnotationViewMKPinAnnotationView 2markerTintColor → pinTintColor 3animatesWhenAdded → animatesDrop

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問