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}
あなたの回答
tips
プレビュー