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

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

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

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

Swift

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

Q&A

0回答

381閲覧

MKMapViewとLong Press Gesture Recognizerを使用して経路が検索できる地図アプリを作る

shisen-t

総合スコア21

Xcode

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

Swift

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

0グッド

0クリップ

投稿2019/04/04 08:26

編集2019/04/05 00:54

MKMapViewを用いて地図アプリを製作しようと考えています。
またMap上を長押しするとピンが刺さり現在地からピンまでの経路を示すようにしたいと考えています。
下記のコードを書いたところビルドは成功して動作中にエラーが出る事もないのですが、長押しをしてもピンが刺さらず経路を示すことができませんでした。
Long Press Gesture Recognizerをもう一度MapView上に配置し直したり、コードを確認したりしたのですが解決方法がわからず質問させていただいた次第です。

コンソールには
[Warning] WARNING: A Gesture recognizer (<UILongPressGestureRecognizer: 0x10090b670; state = Possible; view = <MKMapView 0x101028400>; target= <(action=pressMap:, target=<test.ViewController 0x10090b9e0>)>>) was setup in a storyboard/xib to be added to more than one view (-><MKMapView: 0x101028400; frame = (0 20; 375 556); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x280db0cf0>; layer = <CALayer: 0x28030d0a0>>) at a time, this was never allowed, and is now enforced. Beginning with iOS 9.0 it will be put in the first view it is loaded into.
と表示されているため、Long Pressに問題があるものと考えています。

どなたかお分かりになる方がいらっしゃいましたら、お力添えいただけますと幸いです。

Swift

1コード 2import UIKit 3import MapKit 4 5class ViewController: UIViewController, MKMapViewDelegate { 6 7 @IBOutlet weak var testMapView: MKMapView! 8 9 var testManager: CLLocationManager = CLLocationManager() 10 11 12 @IBAction func pressMap(_ sender: UILongPressGestureRecognizer) { 13 //長押しタップした位置を取得 14 let location: CGPoint = sender.location(in: testMapView) 15 16 if (sender.state == UIGestureRecognizer.State.ended){ 17 //タップした位置を緯度経度の座標に変換 18 let mapPoint: CLLocationCoordinate2D = testMapView.convert(location, toCoordinateFrom: testMapView) 19 20 //ピンを生成 21 let annotation: MKPointAnnotation = MKPointAnnotation() 22 //座標を設定 23 annotation.coordinate = mapPoint 24 //タイトルを設定 25 annotation.title = "目的地" 26 //サブタイトルを設定 27 annotation.subtitle = "ボタンタップで経路を表示" 28 // MapViewにピンを追加 29 testMapView.addAnnotation(annotation) 30 } 31 } 32 33 func mapView(mapView: MKMapView, viewFor annotation: MKPointAnnotation) -> MKAnnotationView? { 34 35 36 let annotationIdentifier = "annotationIdentifier" 37 //ピンを生成 38 let testView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 39 //コールアウト(吹き出し)を表示する 40 testView.canShowCallout = true 41 //アニメーションをつける 42 testView.animatesDrop = true 43 // annotationを設定 44 testView.annotation = annotation 45 46 //annotationViewに経路ボタンを追加 47 let button = UIButton() 48 button.frame = CGRect(x: 0,y: 0,width: 40,height: 30) 49 button.setTitle("経路", for: .normal) 50 button.backgroundColor = UIColor.blue 51 button.setTitleColor(UIColor.white, for: .highlighted) 52 testView.rightCalloutAccessoryView = button 53 54 return testView 55 56 } 57 58 //経路ボタンが押されたとき 59 func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 60 //目的地の位置情報を作る 61 let coordinate = CLLocationCoordinate2DMake(view.annotation!.coordinate.latitude, view.annotation!.coordinate.latitude) 62 let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil) 63 let mapItem = MKMapItem(placemark: placemark) 64 65 //起動オプション 66 let option: [String:AnyObject] = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving as AnyObject, MKLaunchOptionsMapTypeKey: MKMapType.hybrid.rawValue as AnyObject] 67 68 //マップアプリを起動 69 mapItem.openInMaps(launchOptions: option) 70 71 } 72 73 74 75 76 77 78 79 override func viewDidLoad() { 80 super.viewDidLoad() 81 // Do any additional setup after loading the view. 82 83 //デリゲート先を自分に設定する。 84 testManager.delegate = self 85 86 //位置情報の利用許可を変更する画面をポップアップ表示する。 87 testManager.requestWhenInUseAuthorization() 88 89 //位置情報の取得を要求する。 90 testManager.requestLocation() 91 } 92 93 94 95 override func didReceiveMemoryWarning() { 96 super.didReceiveMemoryWarning() 97 // Dispose of any resources that can be recreated. 98 } 99 100 101 102} 103 104 105 106extension ViewController: CLLocationManagerDelegate { 107 //位置情報取得時の呼び出しメソッド 108 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ 109 110 for location in locations { 111 //現在位置をマップの中心にして登録する。 112 let center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 113 let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) // 114 let region = MKCoordinateRegion(center: center, span: span) 115 testMapView.setRegion(region, animated:true) 116 } 117 } 118 119 120 121 //位置情報取得失敗時の呼び出しメソッド 122 func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 123 print(error) 124 } 125 126}

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

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

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

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

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

t_obara

2019/04/04 10:38

Long Pressが認識されないのか、ピンを置くことができないのかは切り分けできているのですか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問