teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードの追加

2020/05/14 05:08

投稿

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