###前提・実現したいこと
アノテーションのタップ時にピンの画像を変更することは出来るのでしょうか。
viewForAnnotation
で画像を変更する方法は実装済みで、
更にここから選択時に画像を変更したいという質問になります。
###該当のソースコード
Swift
1func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 2 view.image = UIImage(named: "sample") 3 }
###試したこと
didSelectAnnotationView
のメソッドは定義済みですが、タップしてもなんの反応もしません。
イメージとしては某赤いアイコンのグルメサイトのアプリです。
###補足情報(言語/FW/ツール等のバージョンなど)
・Xcode7.3.1
・Swift2.2
###全文公開
Swift
1class ViewController2: UIViewController, MKMapViewDelegate { 2 3 @IBOutlet weak var mapView: MKMapView! 4 5 var data: [[String:AnyObject?]] = [] 6 7 override func viewWillAppear(animated: Bool) { 8 super.viewWillAppear(animated) 9 self.navigationItem.title = "概要" 10 } 11 12 override func viewDidAppear(animated: Bool) { 13 super.viewDidAppear(animated) 14 alamoNetwork() 15 } 16 17 func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 18 19 let customAnnotation = annotation as? MapAnnotationSetting 20 21 let identifier = "annotation" 22 if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("annotation") { 23 return annotationView 24 } else { 25 if customAnnotation?.pinImage != nil { 26 let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 27 annotationView.annotation = annotation 28 annotationView.canShowCallout = true 29 annotationView.image = UIImage(named: (customAnnotation?.pinImage)!) 30 31 return annotationView 32 } else { 33 return nil 34 } 35 } 36 } 37 38 func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 39 view.image = UIImage(named: "sample") 40 } 41 42 func dropPin() { 43 if self.data.count > 0 { 44 for i in 0..<data.count { 45 let latitude = self.data[i]["latitude"] as? Double 46 let longitude = self.data[i]["longitude"] as? Double 47 48 let annotation = MapAnnotationSetting() 49 annotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!) 50 annotation.pinImage = "map_01" 51 52 mapView.addAnnotation(annotation) 53 } 54 mapView.showAnnotations(mapView.annotations, animated: true) 55 } 56 } 57 58 func alamoNetwork() { 59 ※省略※ 60 }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。