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

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

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

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

Q&A

解決済

2回答

935閲覧

Swift アノテーションビューが表示されない

globalplus

総合スコア119

Swift

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

0グッド

0クリップ

投稿2019/12/17 02:26

座標から割り出した赤いピンは実装してあるのですが、アノテーションが表示されません。何か間違っているところはありますでしょうか?

@IBOutlet weak var mapView: MKMapView! func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { //アノテーションビューを作成する。 let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil) //吹き出しを表示可能にする。 pinView.canShowCallout = true //ボタン追加 let button = UIButton() button.frame = CGRect(x: 0,y: 0,width: 100,height: 100) button.setTitle("削除", for: .normal) button.backgroundColor = UIColor.red button.setTitleColor(UIColor.white, for:.normal) //経路(省略) button.addTarget(self, action: #selector(self.buttonEvent(_:)), for: UIControl.Event.touchUpInside) pinView.rightCalloutAccessoryView = button return pinView }

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

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

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

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

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

guest

回答2

0

ベストアンサー

別の方法だから別の回答にします。
このMapKiのチュートリアルは完璧だと思いますので、こちらで残しておきます。MapKit Tutorial: Getting Started
ちなみに、登録すればソースコードもダウンロードできます。登録は無料だから安心してください。
イメージ説明

投稿2019/12/18 01:29

vanderlvov

総合スコア685

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

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

globalplus

2019/12/23 17:37

返信遅れてすみませんようやく解読し、見事表示させる事ができました! ありがとうございます。これからもよかったらよろしくお願いします。
guest

0

2点を確認してください:
0. ViewControllerはMKMapViewDelegateを継承するか
0. self.mapView.delegate = self - Delegateを設定しましたか。

動いているコード(カスタムアノテーションビューのサンプル)

swift

1// 2// MapKitViewController.swift 3// mapApp 4// 5// Created by リヴォーフ ユーリ on 2019/12/17. 6// Copyright © 2019 リヴォーフ ユーリ. All rights reserved. 7// 8 9import UIKit 10import MapKit 11 12class MapKitViewController: UIViewController, MKMapViewDelegate { 13 14 @IBOutlet weak var mapView: MKMapView! 15 var placemark: CLPlacemark? = nil 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 let pin = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotation)) 20 pin.minimumPressDuration = 1.0 21 mapView.addGestureRecognizer(pin) 22 self.mapView.delegate = self 23 } 24 25 @objc func addAnnotation(gestureRecognizer:UIGestureRecognizer){ 26 if gestureRecognizer.state == UIGestureRecognizer.State.began { 27 let touchPoint = gestureRecognizer.location(in: mapView) 28 let newCoordinates = mapView.convert(touchPoint, toCoordinateFrom: mapView) 29 let annotation = MKPointAnnotation() 30 annotation.coordinate = newCoordinates 31 32 CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: newCoordinates.latitude, longitude: newCoordinates.longitude), completionHandler: {(placemarks, error) -> Void in 33 if error != nil { 34 print("Reverse geocoder failed with error" + error!.localizedDescription) 35 return 36 } 37 38 if placemarks!.count > 0 { 39 self.placemark = placemarks![0] 40 self.mapView.addAnnotation(annotation) 41 } 42 else { 43 annotation.title = "Unknown Place" 44 self.mapView.addAnnotation(annotation) 45 print("Problem with the data received from geocoder") 46 } 47 } 48 ) 49 } 50 } 51 52 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 53 // Don't want to show a custom image if the annotation is the user's location. 54 guard !(annotation is MKUserLocation) else { 55 return nil 56 } 57 58 // Better to make this class property 59 let annotationIdentifier = "AnnotationIdentifier" 60 61 var annotationView: MKAnnotationView? 62 if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) { 63 annotationView = dequeuedAnnotationView 64 annotationView?.annotation = annotation 65 } 66 else { 67 let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 68 av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 69 annotationView = av 70 } 71 72 if let annotationView = annotationView { 73 // Configure your annotation view here 74 annotationView.canShowCallout = true 75 let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20)); 76 imageView.image = UIImage(named: "lovedeathrobots") 77 annotationView.addSubview(imageView) 78 79 let label = UILabel(frame: CGRect(x: 0, y: 10, width: 100, height: 30)) 80 label.textColor = .black 81 label.adjustsFontSizeToFitWidth = true 82 label.alpha = 0.8 83 label.text = (placemark?.thoroughfare ?? "") + " " + (placemark?.subThoroughfare ?? "") 84 annotationView.addSubview(label) 85 } 86 87 return annotationView 88 } 89} 90

デモアプリ:
https://github.com/vanderlvoff/mapApp

アプリを実行したら、
イメージ説明を押して、MapKitの画面に行けます。

投稿2019/12/17 06:05

vanderlvov

総合スコア685

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

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

globalplus

2019/12/17 20:37

アドバイスありがとうございます。 上記のコードを実行してみるとマップ上に長押ししたところにピンとそのタイトルが表示されています。 実装したい機能としてはピンを押すと吹き出しが出る様にしたいです。 そして2点の確認ですが ViewControllerはMKMapViewDelegateを継承しています self.mapView.delegate = self - Delegateを設定しています
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問