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