質問編集履歴
1
コードの追加
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -1,3 +1,126 @@
|
|
|
1
1
|
swift初心者のものです。よろしくお願いします。
|
|
2
2
|
mapviewの上にボタンを置く際に、mapviewの階層に入らないようにする方法というのはどのようにして行えば良いのでしょうか。
|
|
3
|
-
お手数ですがよろしくお願いします。
|
|
3
|
+
お手数ですがよろしくお願いします。
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import UIKit
|
|
8
|
+
import MapKit
|
|
9
|
+
import CoreLocation
|
|
10
|
+
|
|
11
|
+
class ViewController: UIViewController ,MKMapViewDelegate,
|
|
12
|
+
CLLocationManagerDelegate{
|
|
13
|
+
|
|
14
|
+
var locationManager = CLLocationManager()
|
|
15
|
+
|
|
16
|
+
override func viewDidLoad() {
|
|
17
|
+
super.viewDidLoad()
|
|
18
|
+
|
|
19
|
+
locationManager.delegate = self
|
|
20
|
+
// 画面背景色を設定
|
|
21
|
+
self.view.backgroundColor = UIColor(red:0.7,green:0.7,blue:0.7,alpha:1.0)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 画面回転にも対応する
|
|
25
|
+
override func viewDidAppear(_ animated: Bool) {
|
|
26
|
+
var topPadding: CGFloat = 0
|
|
27
|
+
var bottomPadding: CGFloat = 0
|
|
28
|
+
var leftPadding: CGFloat = 0
|
|
29
|
+
var rightPadding: CGFloat = 0
|
|
30
|
+
|
|
31
|
+
if #available(iOS 11.0, *) {
|
|
32
|
+
// 'keyWindow' was deprecated in iOS 13.0: Should not be used for applications
|
|
33
|
+
let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
|
|
34
|
+
topPadding = window!.safeAreaInsets.top
|
|
35
|
+
bottomPadding = window!.safeAreaInsets.bottom
|
|
36
|
+
leftPadding = window!.safeAreaInsets.left
|
|
37
|
+
rightPadding = window!.safeAreaInsets.right
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
//位置情報サービスの確認
|
|
41
|
+
CLLocationManager.locationServicesEnabled()
|
|
42
|
+
|
|
43
|
+
// セキュリティ認証のステータス
|
|
44
|
+
let status = CLLocationManager.authorizationStatus()
|
|
45
|
+
|
|
46
|
+
if(status == CLAuthorizationStatus.notDetermined) {
|
|
47
|
+
print("NotDetermined")
|
|
48
|
+
// 許可をリクエスト
|
|
49
|
+
locationManager.requestWhenInUseAuthorization()
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
else if(status == CLAuthorizationStatus.restricted){
|
|
53
|
+
print("Restricted")
|
|
54
|
+
}
|
|
55
|
+
else if(status == CLAuthorizationStatus.authorizedWhenInUse){
|
|
56
|
+
print("authorizedWhenInUse")
|
|
57
|
+
}
|
|
58
|
+
else if(status == CLAuthorizationStatus.authorizedAlways){
|
|
59
|
+
print("authorizedAlways")
|
|
60
|
+
}
|
|
61
|
+
else{
|
|
62
|
+
print("not allowed")
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 位置情報の更新
|
|
66
|
+
locationManager.startUpdatingLocation()
|
|
67
|
+
|
|
68
|
+
// MapViewのインスタンス生成.
|
|
69
|
+
let mapView = MKMapView()
|
|
70
|
+
|
|
71
|
+
// MapViewをSafeAreaに収める(Portraitのケース)
|
|
72
|
+
// 以降、Landscape のみを想定
|
|
73
|
+
let screenWidth = view.frame.size.width
|
|
74
|
+
let screenHeight = view.frame.size.height
|
|
75
|
+
|
|
76
|
+
let rect = CGRect(x: leftPadding,
|
|
77
|
+
y: topPadding,
|
|
78
|
+
width: screenWidth - leftPadding - rightPadding,
|
|
79
|
+
height: screenHeight - topPadding - bottomPadding )
|
|
80
|
+
|
|
81
|
+
mapView.frame = rect
|
|
82
|
+
|
|
83
|
+
// Delegateを設定.
|
|
84
|
+
mapView.delegate = self
|
|
85
|
+
|
|
86
|
+
// 縮尺を設定
|
|
87
|
+
var region:MKCoordinateRegion = mapView.region
|
|
88
|
+
region.center = mapView.userLocation.coordinate
|
|
89
|
+
|
|
90
|
+
region.span.latitudeDelta = 0.04
|
|
91
|
+
region.span.longitudeDelta = 0.04
|
|
92
|
+
|
|
93
|
+
mapView.setRegion(region,animated:true)
|
|
94
|
+
// MapViewをViewに追加.
|
|
95
|
+
self.view.addSubview(mapView)
|
|
96
|
+
|
|
97
|
+
mapView.mapType = MKMapType.hybrid
|
|
98
|
+
|
|
99
|
+
mapView.userTrackingMode = MKUserTrackingMode.follow
|
|
100
|
+
mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
|
|
105
|
+
print("region changed")
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@IBAction func plusbutton(_ sender: Any) {
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
}
|