いつもお世話になります。
MarkerAnnotationViewをドラッグで移動させる際に、Markerが消えてしまいますが、
PinAnnotationViewのときのようにドラッグ中にCalloutを非表示にし、Markerを表示させるようにするにはどうすればいいでしょうか?
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
1MKMarkerAnnotationView → MKPinAnnotationView 2markerTintColor → pinTintColor 3animatesWhenAdded → animatesDrop
あなたの回答
tips
プレビュー