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

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

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

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

Swift

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

Q&A

解決済

1回答

1109閲覧

mapViewの中心にピンを表示させたい

Raleigh

総合スコア8

Xcode

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

Swift

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

0グッド

0クリップ

投稿2020/09/23 08:01

前提・実現したいこと

MapKitで地図アプリを開発しています。そこで地図にピンを刺したいのですが、
タップなどでピンを刺すのではなく、常に地図の中心にピンが表示されていてピンを刺したいところに地図の中心を合わせることでピンを刺したいです。
もしそのような機能がMapKitに存在するのであれば、教えてくださると大変助かります。

イメージとしては、下の画像のようにピンが表示されている状況で地図を移動させ、ピンを指す場所を選択する感じです。
イメージ図

試したこと

ピンの画像をネットから取ってきて、表示する方法も考えましたが不自然なUIになりやめました。

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

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

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

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

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

TsukubaDepot

2020/09/23 11:50

常に地図(に相当するView)の中心にピン(のような画像)を表示する、という方法では何らかの不都合があるのでしょうか。 「不自然なUI」とあるので、なんらかの不都合があるのかと思いますが、それも詳しく書いていただいた方がより良い回答が得られるようにおもめます。
Raleigh

2020/09/23 12:34

すいません。質問が適切ではなかったです。求めていた画像が探してもなかったことと、ピンの指すところと地図の中心を合わせることが難しそうという理由で、楽をしようとMapKitにそのような機能がないか安易に質問をしてしまいました。やはりMapKitにそのような機能はなさそうなので、画像を表示する方向で頑張ろうと思います。ありがとうございます。
TsukubaDepot

2020/09/23 12:35

いや、直接的な機能はありませんが、おそらく実現できそうな方法はあります。 MKMapView の delegate と、GestureRecoqnizer を併用する方法です。
Raleigh

2020/09/23 12:39

GestureRecoqnizerというのは詳しい使い方はわからないのですが、 一度指したピンをスワイプで移動させるといった感じでしょうか?
guest

回答1

0

ベストアンサー

考えていいらっしゃる方法がこのような方法かわかりませんが、ドラッグに合わせてピンも移動します。

イメージ説明

GestureRecognizer を併用せず、MapKit の delegate だけだと、移動完了後しかピンが移動しません。

ただし、現状の実装だとドラッグ時にジッタが発生し、多少見苦しいので、その辺りはぜひ質問者さんでうまい方法を考えていただければと思います。

Swift

1import UIKit 2import MapKit 3 4// UIGestureRecognizerDelegate に準拠させる 5class ViewController: UIViewController, MKMapViewDelegate, UIGestureRecognizerDelegate { 6 @IBOutlet weak var mapView: MKMapView! 7 8 var annotationCoordinate = MKPointAnnotation() 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view. 13 14 mapView.delegate = self 15 16 // mapView に gestureRecognizer を追加する 17 let gesture = UIPanGestureRecognizer(target: self, action: #selector(receiveGesture(_:))) 18 gesture.delegate = self 19 mapView.addGestureRecognizer(gesture) 20 21 // 緯度・経度を設定 22 let location = CLLocationCoordinate2DMake(35.68154,139.752498) 23 mapView.setCenter(location, animated:true) 24 25 // 初期のアノテーション 26 annotationCoordinate.coordinate = location 27 mapView.addAnnotation(annotationCoordinate) 28 29 // 縮尺を設定 30 var region = mapView.region 31 region.center = location 32 region.span.latitudeDelta = 0.02 33 region.span.longitudeDelta = 0.02 34 35 mapView.setRegion(region,animated:true) 36 mapView.mapType = .standard 37 } 38 39 // delegate: 複数の GestureRecognizer を有効にする 40 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 41 return true 42 } 43 44 @objc func receiveGesture(_ gestureRecognizer : UIPanGestureRecognizer) { 45 print(#function) 46 if let mapView = gestureRecognizer.view as? MKMapView { 47 moveAnnotation(mapView) 48 } 49 } 50 51 func moveAnnotation(_ mapView: MKMapView) { 52 // 地図の中央座標 53 annotationCoordinate.coordinate = mapView.centerCoordinate 54 } 55 56 // delegate: ドラッグ終了 57 func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) { 58 print(#function) 59 moveAnnotation(mapView) 60 } 61 62 // delegate: ドラッグ開始 63 func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) { 64 print(#function) 65 } 66}

##任意の View を表示させる方法

Swift

1import UIKit 2import MapKit 3 4// UIGestureRecognizerDelegate に準拠させる 5class ViewController: UIViewController, MKMapViewDelegate, UIGestureRecognizerDelegate { 6 @IBOutlet weak var mapView: MKMapView! 7 8 var annotationCoordinate = MKPointAnnotation() 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view. 13 14 mapView.delegate = self 15 16 // mapView に gestureRecognizer を追加する 17 let gesture = UIPanGestureRecognizer(target: self, action: #selector(receiveGesture(_:))) 18 gesture.delegate = self 19 mapView.addGestureRecognizer(gesture) 20 21 // 緯度・経度を設定 22 let location = CLLocationCoordinate2DMake(35.68154,139.752498) 23 mapView.setCenter(location, animated:true) 24 25 // 初期のアノテーション 26 annotationCoordinate.coordinate = location 27 mapView.addAnnotation(annotationCoordinate) 28 29 // 縮尺を設定 30 var region = mapView.region 31 region.center = location 32 region.span.latitudeDelta = 0.02 33 region.span.longitudeDelta = 0.02 34 35 mapView.setRegion(region,animated:true) 36 mapView.mapType = .standard 37 38 // アニメーションを使うなら 39 // 縦横20ピクセルの視覚を用意する 40 let squareView = UIView(frame: .init(x: 0, y: 0, width: 20, height: 20)) 41 squareView.layer.borderWidth = 2 42 squareView.layer.borderColor = UIColor.red.cgColor 43 44 // 点滅処理 45 UIView.animateKeyframes(withDuration: 0.5, delay: 0.0, options: [.autoreverse, .repeat]) { 46 squareView.alpha = 0.0 47 } completion: { _ in 48 } 49 50 // mapView の region.center として設定されている CGPoint を取得して、squareView の中心にする 51 squareView.center = mapView.convert(mapView.region.center, toPointTo: mapView) 52 mapView.addSubview(squareView) 53 } 54 55 // delegate: 複数の GestureRecognizer を有効にする 56 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 57 return true 58 } 59 60 @objc func receiveGesture(_ gestureRecognizer : UIPanGestureRecognizer) { 61 print(#function) 62 if let mapView = gestureRecognizer.view as? MKMapView { 63 moveAnnotation(mapView) 64 } 65 } 66 67 func moveAnnotation(_ mapView: MKMapView) { 68 // 地図の中央座標 69 annotationCoordinate.coordinate = mapView.centerCoordinate 70 } 71 72 // delegate: ドラッグ終了 73 func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) { 74 print(#function) 75 moveAnnotation(mapView) 76 } 77 78 // delegate: ドラッグ開始 79 func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) { 80 print(#function) 81 } 82}

投稿2020/09/23 12:47

編集2020/09/23 15:02
TsukubaDepot

総合スコア5086

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

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

Raleigh

2020/09/23 13:11

回答ありがとうございます! わざわざ方法を考えてくださってありがとうございます。 画像を表示する方と同時に進めていき、両方とも試していこうと思います!
TsukubaDepot

2020/09/23 15:03

任意の View をMapView の中心(View の中心ではなく、地図座標としての中心)に重ねて表示させる方法も追記しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問