質問編集履歴

1

コードの追加

2020/05/14 05:08

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -11,3 +11,211 @@
11
11
  お手数ですがよろしくお願いします。
12
12
 
13
13
  ![イメージ説明](f11d218ef189957d555e424cd2450536.png)
14
+
15
+
16
+
17
+ こちらがコードになります
18
+
19
+ import UIKit
20
+
21
+ import MapKit
22
+
23
+ import CoreLocation
24
+
25
+
26
+
27
+ class ViewController: UIViewController ,MKMapViewDelegate,
28
+
29
+ CLLocationManagerDelegate{
30
+
31
+
32
+
33
+ var locationManager = CLLocationManager()
34
+
35
+
36
+
37
+ override func viewDidLoad() {
38
+
39
+ super.viewDidLoad()
40
+
41
+
42
+
43
+ locationManager.delegate = self
44
+
45
+ // 画面背景色を設定
46
+
47
+ self.view.backgroundColor = UIColor(red:0.7,green:0.7,blue:0.7,alpha:1.0)
48
+
49
+ }
50
+
51
+
52
+
53
+ // 画面回転にも対応する
54
+
55
+ override func viewDidAppear(_ animated: Bool) {
56
+
57
+ var topPadding: CGFloat = 0
58
+
59
+ var bottomPadding: CGFloat = 0
60
+
61
+ var leftPadding: CGFloat = 0
62
+
63
+ var rightPadding: CGFloat = 0
64
+
65
+
66
+
67
+ if #available(iOS 11.0, *) {
68
+
69
+ // 'keyWindow' was deprecated in iOS 13.0: Should not be used for applications
70
+
71
+ let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
72
+
73
+ topPadding = window!.safeAreaInsets.top
74
+
75
+ bottomPadding = window!.safeAreaInsets.bottom
76
+
77
+ leftPadding = window!.safeAreaInsets.left
78
+
79
+ rightPadding = window!.safeAreaInsets.right
80
+
81
+ }
82
+
83
+
84
+
85
+ //位置情報サービスの確認
86
+
87
+ CLLocationManager.locationServicesEnabled()
88
+
89
+
90
+
91
+ // セキュリティ認証のステータス
92
+
93
+ let status = CLLocationManager.authorizationStatus()
94
+
95
+
96
+
97
+ if(status == CLAuthorizationStatus.notDetermined) {
98
+
99
+ print("NotDetermined")
100
+
101
+ // 許可をリクエスト
102
+
103
+ locationManager.requestWhenInUseAuthorization()
104
+
105
+
106
+
107
+ }
108
+
109
+ else if(status == CLAuthorizationStatus.restricted){
110
+
111
+ print("Restricted")
112
+
113
+ }
114
+
115
+ else if(status == CLAuthorizationStatus.authorizedWhenInUse){
116
+
117
+ print("authorizedWhenInUse")
118
+
119
+ }
120
+
121
+ else if(status == CLAuthorizationStatus.authorizedAlways){
122
+
123
+ print("authorizedAlways")
124
+
125
+ }
126
+
127
+ else{
128
+
129
+ print("not allowed")
130
+
131
+ }
132
+
133
+
134
+
135
+ // 位置情報の更新
136
+
137
+ locationManager.startUpdatingLocation()
138
+
139
+
140
+
141
+ // MapViewのインスタンス生成.
142
+
143
+ let mapView = MKMapView()
144
+
145
+
146
+
147
+ // MapViewをSafeAreaに収める(Portraitのケース)
148
+
149
+ // 以降、Landscape のみを想定
150
+
151
+ let screenWidth = view.frame.size.width
152
+
153
+ let screenHeight = view.frame.size.height
154
+
155
+
156
+
157
+ let rect = CGRect(x: leftPadding,
158
+
159
+ y: topPadding,
160
+
161
+ width: screenWidth - leftPadding - rightPadding,
162
+
163
+ height: screenHeight - topPadding - bottomPadding )
164
+
165
+
166
+
167
+ mapView.frame = rect
168
+
169
+
170
+
171
+ // Delegateを設定.
172
+
173
+ mapView.delegate = self
174
+
175
+
176
+
177
+ // 縮尺を設定
178
+
179
+ var region:MKCoordinateRegion = mapView.region
180
+
181
+ region.center = mapView.userLocation.coordinate
182
+
183
+
184
+
185
+ region.span.latitudeDelta = 0.02
186
+
187
+ region.span.longitudeDelta = 0.02
188
+
189
+
190
+
191
+ mapView.setRegion(region,animated:true)
192
+
193
+ // MapViewをViewに追加.
194
+
195
+ self.view.addSubview(mapView)
196
+
197
+
198
+
199
+ mapView.mapType = MKMapType.hybrid
200
+
201
+
202
+
203
+ mapView.userTrackingMode = MKUserTrackingMode.follow
204
+
205
+ mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
206
+
207
+
208
+
209
+ }
210
+
211
+
212
+
213
+ func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
214
+
215
+ print("region changed")
216
+
217
+ }
218
+
219
+
220
+
221
+ }