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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

925閲覧

MKMarkerAnnotationViewでエラーが発生してしまう Terminating app due to uncaught exception

yoheionishi

総合スコア5

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2020/06/14 01:24

編集2020/06/14 01:30

事象

地図にアノテーションを表示すると、ビルド、実行はできるが、実行中にエラーが発生して停止してしまう。
MKMarkerAnnotationViewで、クラスタリングやglyphimageの表示を行っている。
アノテーションは100個程度で、Viewのロード時にFirestoreから取得して配列に格納している。
エラーは、地図の拡大や移動時に発生している。
Swift version: Swift5
outlet接続も確認したが問題なさそうに見えます。

解決策が見出せず、、何が問題なのか教えていただけないでしょうか。
Mapの設定方法等については色々調べながら書いたのですが、根本的に間違っていたらご教授いただけると大変ありがたいです。

エラー

[XXX.CustomePinAnnotation memberAnnotations]: unrecognized selector sent to instance 0x2835e16c0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XXX.CustomePinAnnotation memberAnnotations]: unrecognized selector sent to instance 0x2835e16c0'
*** First throw call stack:

CustomePinAnnotation.swift

Swift

1import UIKit 2import MapKit 3 4class CustomePinAnnotation: NSObject, MKAnnotation { 5 let clusteringIdentifier : String 6 let title: String? 7 let subtitle: String? 8 let coordinate: CLLocationCoordinate2D 9 //let glyphText: String 10 let glyphImage: UIImage 11 let glyphTintColor: UIColor 12 let markerTintColor: UIColor 13 let objectid: Int 14 init(_ clusteringIdentifier: String, title: String, subtitle: String, coordinate: CLLocationCoordinate2D, glyphImage: UIImage, glyphTintColor: UIColor = .white, markerTintColor: UIColor, objectid: Int) { 15 self.clusteringIdentifier = clusteringIdentifier 16 self.title = title 17 self.subtitle = subtitle 18 self.coordinate = coordinate 19// self.glyphText = glyphText 20 self.glyphImage = glyphImage 21 self.glyphTintColor = glyphTintColor 22 self.markerTintColor = markerTintColor 23 self.objectid = objectid 24 } 25}

ViewController.swift

import UIKit import MapKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { var locationManager = CLLocationManager() var userCoordinate = CLLocationCoordinate2D() //object配列 var objectAll = [objectData]() // マップビュー @IBOutlet weak var mapView: MKMapView! func displayAllMountains() { for mountain in objectAll { // カスタムクラスで、ピンの初期設定をする let pinImage = UIImage.init(named: "XXXXX")! let subtitletext = String(object.height) + "m" let annotation = YamaPinAnnotation("clusterid", title:object.name, subtitle: subtitletext, coordinate: object.geopoint, glyphImage: pinImage, glyphTintColor: .white, markerTintColor: .darkGray, objectid: object.objectid) self.mapView.addAnnotation(annotation) } } //addAnnotation時呼ばれるデリゲートメソッド func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotation === mapView.userLocation {      // 現在地を示すアノテーションの場合はデフォルトのまま return nil } else { let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: annotation) guard let markerAnnotationView = annotationView as? MKMarkerAnnotationView, let annotation = annotation as? YamaPinAnnotation else { return annotationView } markerAnnotationView.clusteringIdentifier = annotation.clusteringIdentifier // markerAnnotationView.glyphText = annotation.glyphText markerAnnotationView.glyphImage = annotation.glyphImage markerAnnotationView.glyphTintColor = annotation.glyphTintColor markerAnnotationView.markerTintColor = annotation.markerTintColor return markerAnnotationView } } }

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

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

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

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

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

guest

回答1

0

自己解決

自己解決できました。

エラーが出ていた原因は、本来MKClusterAnnotationを使用するべき場所で、MKAnnotationを拡張したCustomPinAnnotationを使っていたこと。

MKUserLocationとCustomPinAnnotationだけでなく、MKClusterAnnotationも正しく管理していなかったため、ClusterAnnotationのMemberAnnotationでエラーが発生していたようです。

解決策として
mapView(_ mapView: MKMapView, viewFor annotation:
の中で、MKClusterAnnotation の更新時の挙動を追記してあげることが1つですが、
今回は上記を削除して、新しくannotation view classを定義しました。

MKMarkerAnnotationViewについて日本語のブログや記事を見てコードを書いていましたが、MKClusterAnnotationのハンドリングについて記載しているものはなく、かなり苦戦しましたが、最初からMKMarkerAnnotationのドキュメントをしっかり読んでおけば苦労することもなかったのかも・・・

Swift

1mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier) 2mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier) 3 4 5class CustomAnnotationView: MKMarkerAnnotationView { 6 override init(annotation: MKAnnotation?, reuseIdentifier: String?) { 7 super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) 8 update(for: annotation) 9 } 10 11 required init?(coder aDecoder: NSCoder) { 12 fatalError("init(coder:) has not been implemented") 13 } 14 15 override var annotation: MKAnnotation? { didSet { update(for: annotation) } } 16 17 func update(for annotation: MKAnnotation?) { 18 displayPriority = .required 19 20 guard let annotation = annotation as? CustomPinAnnotation else { return } 21 22 clusteringIdentifier = annotation.clusteringIdentifier 23 // markerAnnotationView.glyphText = annotation.glyphText 24 glyphImage = annotation.glyphImage 25 glyphTintColor = annotation.glyphTintColor 26 markerTintColor = annotation.markerTintColor 27 } 28}

投稿2020/06/30 12:47

yoheionishi

総合スコア5

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問