回答編集履歴
1
再現確認のコードを追記しました。
test
CHANGED
@@ -4,3 +4,92 @@
|
|
4
4
|
*Xcode 14.3
|
5
5
|
|
6
6
|

|
7
|
+
|
8
|
+
### 追記です。
|
9
|
+
|
10
|
+
|
11
|
+
> こちらどこに書けば上記のように複数のAnnotationをマッピングできますでしょうか??
|
12
|
+
|
13
|
+
コメントありがとうございます。
|
14
|
+
|
15
|
+
質問用にコードを修正しているのだとしたら、
|
16
|
+
その違いによる影響もあるかもしれませんが、
|
17
|
+
「該当のソースコード」を修正して再現確認したコードは次の通りになります。
|
18
|
+
|
19
|
+
```swift
|
20
|
+
import UIKit
|
21
|
+
import MapKit
|
22
|
+
import CoreLocation
|
23
|
+
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
|
24
|
+
// モデル
|
25
|
+
struct UserInfo: Codable {
|
26
|
+
var title: String
|
27
|
+
var subtitle: String
|
28
|
+
var latitude: Double
|
29
|
+
var longitude: Double
|
30
|
+
}
|
31
|
+
// 接続
|
32
|
+
@IBOutlet weak var mapView: MKMapView!
|
33
|
+
// 変数:地図に立てるピン
|
34
|
+
var locationManager: CLLocationManager!
|
35
|
+
// 地図上に表示するユーザーを入れる配列
|
36
|
+
var displayUserInfos: [UserInfo] = []
|
37
|
+
override func viewDidLoad() {
|
38
|
+
super.viewDidLoad()
|
39
|
+
// ロングタップを検知
|
40
|
+
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(recognizeLongPress(sender:)))
|
41
|
+
//MapViewにリスナーを登録
|
42
|
+
self.mapView.addGestureRecognizer(longPress)
|
43
|
+
}
|
44
|
+
// MARK: - Action
|
45
|
+
//ロングタップした時に呼ばれる関数
|
46
|
+
@objc func recognizeLongPress(sender: UILongPressGestureRecognizer) {
|
47
|
+
//長押し感知は最初の1回のみ
|
48
|
+
if sender.state != UIGestureRecognizer.State.began {
|
49
|
+
return
|
50
|
+
}
|
51
|
+
// 位置情報を取得
|
52
|
+
let location = sender.location(in: self.mapView)
|
53
|
+
let coordinate = self.mapView.convert(location, toCoordinateFrom: self.mapView)
|
54
|
+
// タップした位置に照準を合わせる処理
|
55
|
+
let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
|
56
|
+
let region = MKCoordinateRegion(center: coordinate, span: span)
|
57
|
+
self.mapView.region = region
|
58
|
+
// ピンを生成
|
59
|
+
let pin = MKPointAnnotation()
|
60
|
+
pin.title = "タイトル"
|
61
|
+
pin.subtitle = "サブタイトル"
|
62
|
+
// タップした位置情報に位置にピンを追加
|
63
|
+
pin.coordinate = coordinate
|
64
|
+
// 地図にピンを表示
|
65
|
+
self.mapView.addAnnotation(pin)
|
66
|
+
// ユーザー表示
|
67
|
+
self.getUsersFromGeocode()
|
68
|
+
}
|
69
|
+
// MARK: - Function
|
70
|
+
func getUsersFromGeocode() {
|
71
|
+
// ある地点を中心に半径1km以内にいるユーザーを取得する処理
|
72
|
+
for displayUserInfo in displayUserInfos {
|
73
|
+
print("==================================")
|
74
|
+
print("displayUserInfo.latitudeは")
|
75
|
+
print(displayUserInfo.latitude)
|
76
|
+
print("==================================")
|
77
|
+
print("displayUserInfo.latitudeは")
|
78
|
+
print(displayUserInfo.longitude)
|
79
|
+
print("==================================")
|
80
|
+
// ピンの生成
|
81
|
+
let annotation = MKPointAnnotation()
|
82
|
+
// 緯度経度を指定
|
83
|
+
annotation.coordinate = CLLocationCoordinate2DMake(displayUserInfo.latitude, displayUserInfo.longitude)
|
84
|
+
// mapViewに追加
|
85
|
+
self.mapView.addAnnotation(annotation)
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
getUsersFromGeocodeメソッドが
|
92
|
+
MapViewControllerクラスの外側に記述されていたりしましたので、
|
93
|
+
そういった点を修正したりしました。
|
94
|
+
「該当のソースコード」と比較してみてください。
|
95
|
+
|