質問編集履歴

3

内容の詳細とコードを編集

2018/04/02 06:05

投稿

TakayukiNakajo
TakayukiNakajo

スコア23

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
 
20
20
 
21
- 現在地を取得して、特定の位置から離れたらアラートを表示するという機能を実装したのですが、シュミレーターではバックグラウンドで動いているにも関わらず、実機に落とすと動きません。(アプリを開いてホーム画面に戻った状態で、データが更新されません。)
21
+ 現在地を取得して、特定の位置から離れたらアラートを表示するという機能を実装したのですが、シュミレーターではバックグラウンドで動いているにも関わらず、実機に落とすと数百メートル歩いても動きません。(アプリを開いてホーム画面に戻った状態で、データが更新されません。locationManager.distanceFilter = 50を入れておりますので、50メートルごとにバックグラウンドでも更新するように記載もしています。
22
22
 
23
23
 
24
24
 
@@ -110,56 +110,44 @@
110
110
 
111
111
 
112
112
 
113
- // ログインしていなかったら、スキップ
113
+ let location = locations[0]
114
+
115
+
116
+
114
-
117
+ if let newLocation = locations.last{
118
+
115
- let userNameStored = UserDefaults.standard.string(forKey: "userName")
119
+ print("((newLocation.coordinate.latitude), (newLocation.coordinate.latitude))")
116
-
117
- if (nil == userNameStored) {
118
-
119
- return
120
120
 
121
121
  }
122
122
 
123
123
 
124
124
 
125
+ // 住所 (緯度、経度)
126
+
125
- let location = locations[0]
127
+ let lati:Double = 37.33186, longi:Double = -122.030248
126
-
127
-
128
-
128
+
129
- if let newLocation = locations.last{
129
+ let currentLatitude = location.coordinate.latitude // 現在の緯度(中央値)
130
+
130
-
131
+ let currentLongitude = location.coordinate.longitude // 現在の経度(中央値)
132
+
133
+ let span:MKCoordinateSpan = MKCoordinateSpanMake(0.016, 0.016) // 表示している範囲 (0.001で約110.9m)
134
+
131
- print("((newLocation.coordinate.latitude), (newLocation.coordinate.latitude))")
135
+ let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(currentLatitude, currentLongitude)
136
+
137
+ let latitudeDistance = calc(latitude:lati, currentLatitude:currentLatitude) // 緯度が指定の範囲を超えたら1を返す。
138
+
139
+ let longitudeDistance = calc(longitude:longi, currentLongitude:currentLongitude) // 経度が指定の範囲を超えたら1を返す。
140
+
141
+
142
+
143
+ if (0 != (latitudeDistance + longitudeDistance)){
144
+
145
+ // 処理
132
146
 
133
147
  }
134
148
 
135
149
 
136
150
 
137
- // 住所 (緯度、経度)
138
-
139
- let lati:Double = 37.33186, longi:Double = -122.030248
140
-
141
- let currentLatitude = location.coordinate.latitude // 現在の緯度(中央値)
142
-
143
- let currentLongitude = location.coordinate.longitude // 現在の経度(中央値)
144
-
145
- let span:MKCoordinateSpan = MKCoordinateSpanMake(0.016, 0.016) // 表示している範囲 (0.001で約110.9m)
146
-
147
- let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(currentLatitude, currentLongitude)
148
-
149
- let latitudeDistance = calc(latitude:lati, currentLatitude:currentLatitude) // 緯度が指定の範囲を超えたら1を返す。
150
-
151
- let longitudeDistance = calc(longitude:longi, currentLongitude:currentLongitude) // 経度が指定の範囲を超えたら1を返す。
152
-
153
-
154
-
155
- if (0 != (latitudeDistance + longitudeDistance)){
156
-
157
- // 処理
158
-
159
- }
160
-
161
-
162
-
163
151
  let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
164
152
 
165
153
  Map.setRegion(region, animated: true)
@@ -218,36 +206,6 @@
218
206
 
219
207
 
220
208
 
221
- // ログインされているかどうかの判定
222
-
223
- override func viewDidAppear(_ animated: Bool) {
224
-
225
- let isUserLoggedIn = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
226
-
227
- // ログインされていなければ、ログイン画面を表示
228
-
229
- if (!isUserLoggedIn) {
230
-
231
- self.performSegue (withIdentifier: "loginView", sender: self)
232
-
233
- }
234
-
235
- }
236
-
237
-
238
-
239
- // ログアウトボタンを押したら、ログイン画面を表示
240
-
241
- @IBAction func logoutButtonTapped(_ sender: Any) {
242
-
243
- UserDefaults.standard.set(false, forKey: "isUserLoggedIn")
244
-
245
- self.performSegue(withIdentifier: "loginView", sender: self)
246
-
247
- }
248
-
249
-
250
-
251
209
  }
252
210
 
253
211
 

2

記述しているコードを追加

2018/04/02 06:05

投稿

TakayukiNakajo
TakayukiNakajo

スコア23

test CHANGED
File without changes
test CHANGED
@@ -19,3 +19,237 @@
19
19
 
20
20
 
21
21
  現在地を取得して、特定の位置から離れたらアラートを表示するという機能を実装したのですが、シュミレーター場ではバックグラウンドで動いているにも関わらず、実機に落とすと動きません。(アプリを開いてホーム画面に戻った状態で、データが更新されません。)
22
+
23
+
24
+
25
+ こちら、記述しているコードを載せておきます。
26
+
27
+
28
+
29
+ ``` ViewController.swift
30
+
31
+ import UIKit
32
+
33
+ import MapKit
34
+
35
+ import CoreLocation
36
+
37
+
38
+
39
+ // 目的地からの距離を計算する関数(緯度)
40
+
41
+ func calc(latitude:Double, currentLatitude:Double) -> Int{
42
+
43
+ let latitudeDistance = (latitude - currentLatitude)
44
+
45
+ // 330mより離れていたら (0.001 度ではおよそ 110.9m) 1を返す
46
+
47
+ if (latitudeDistance < -0.001 || 0.001 < latitudeDistance) {
48
+
49
+ return 1
50
+
51
+ }
52
+
53
+ return 0
54
+
55
+ }
56
+
57
+
58
+
59
+ // 目的地からの距離を計算する関数(経度)
60
+
61
+ func calc(longitude:Double, currentLongitude:Double) -> Int{
62
+
63
+ let longitudeDistance = longitude - currentLongitude
64
+
65
+ // 330mより離れていたら (0.001 度ではおよそ 110.9m) 1を返す
66
+
67
+ if (longitudeDistance < -0.001 || 0.001 < longitudeDistance){
68
+
69
+ return 1
70
+
71
+ }
72
+
73
+ return 0
74
+
75
+ }
76
+
77
+
78
+
79
+
80
+
81
+ // 現在地を取得して自動退席
82
+
83
+ class ViewController: UIViewController, CLLocationManagerDelegate {
84
+
85
+
86
+
87
+ @IBOutlet weak var label: UILabel!
88
+
89
+ @IBOutlet weak var Map: MKMapView!
90
+
91
+ @IBOutlet weak var latitudeDebug: UILabel!
92
+
93
+ @IBOutlet weak var longitudeDebug: UILabel!
94
+
95
+
96
+
97
+ // NSUserDefaults のインスタンス
98
+
99
+ let userDefaults = UserDefaults.standard
100
+
101
+
102
+
103
+ // locationManagers
104
+
105
+ let locationManager = CLLocationManager()
106
+
107
+
108
+
109
+ func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
110
+
111
+
112
+
113
+ // ログインしていなかったら、スキップ
114
+
115
+ let userNameStored = UserDefaults.standard.string(forKey: "userName")
116
+
117
+ if (nil == userNameStored) {
118
+
119
+ return
120
+
121
+ }
122
+
123
+
124
+
125
+ let location = locations[0]
126
+
127
+
128
+
129
+ if let newLocation = locations.last{
130
+
131
+ print("((newLocation.coordinate.latitude), (newLocation.coordinate.latitude))")
132
+
133
+ }
134
+
135
+
136
+
137
+ // 住所 (緯度、経度)
138
+
139
+ let lati:Double = 37.33186, longi:Double = -122.030248
140
+
141
+ let currentLatitude = location.coordinate.latitude // 現在の緯度(中央値)
142
+
143
+ let currentLongitude = location.coordinate.longitude // 現在の経度(中央値)
144
+
145
+ let span:MKCoordinateSpan = MKCoordinateSpanMake(0.016, 0.016) // 表示している範囲 (0.001で約110.9m)
146
+
147
+ let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(currentLatitude, currentLongitude)
148
+
149
+ let latitudeDistance = calc(latitude:lati, currentLatitude:currentLatitude) // 緯度が指定の範囲を超えたら1を返す。
150
+
151
+ let longitudeDistance = calc(longitude:longi, currentLongitude:currentLongitude) // 経度が指定の範囲を超えたら1を返す。
152
+
153
+
154
+
155
+ if (0 != (latitudeDistance + longitudeDistance)){
156
+
157
+ // 処理
158
+
159
+ }
160
+
161
+
162
+
163
+ let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
164
+
165
+ Map.setRegion(region, animated: true)
166
+
167
+
168
+
169
+ print(location.altitude)
170
+
171
+ print(location.speed)
172
+
173
+
174
+
175
+ self.Map.showsUserLocation = true
176
+
177
+ }
178
+
179
+
180
+
181
+ override func viewDidLoad() {
182
+
183
+ super.viewDidLoad()
184
+
185
+
186
+
187
+ // Keyを指定して読み込み
188
+
189
+ let rec: String = userDefaults.object(forKey: "Record") as! String
190
+
191
+
192
+
193
+ label.text = rec
194
+
195
+ print(rec)
196
+
197
+
198
+
199
+ locationManager.delegate = self
200
+
201
+ locationManager.distanceFilter = 50 // 50m離れたら通知
202
+
203
+ locationManager.requestAlwaysAuthorization() // 常に許可
204
+
205
+ locationManager.startUpdatingLocation() //位置情報を更新
206
+
207
+
208
+
209
+ }
210
+
211
+
212
+
213
+ override func didReceiveMemoryWarning() {
214
+
215
+ super.didReceiveMemoryWarning()
216
+
217
+ }
218
+
219
+
220
+
221
+ // ログインされているかどうかの判定
222
+
223
+ override func viewDidAppear(_ animated: Bool) {
224
+
225
+ let isUserLoggedIn = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
226
+
227
+ // ログインされていなければ、ログイン画面を表示
228
+
229
+ if (!isUserLoggedIn) {
230
+
231
+ self.performSegue (withIdentifier: "loginView", sender: self)
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
239
+ // ログアウトボタンを押したら、ログイン画面を表示
240
+
241
+ @IBAction func logoutButtonTapped(_ sender: Any) {
242
+
243
+ UserDefaults.standard.set(false, forKey: "isUserLoggedIn")
244
+
245
+ self.performSegue(withIdentifier: "loginView", sender: self)
246
+
247
+ }
248
+
249
+
250
+
251
+ }
252
+
253
+
254
+
255
+ ```

1

動かないということに関して、具体的に記述

2018/04/02 05:13

投稿

TakayukiNakajo
TakayukiNakajo

スコア23

test CHANGED
File without changes
test CHANGED
@@ -18,4 +18,4 @@
18
18
 
19
19
 
20
20
 
21
- 現在地を取得して、特定の位置から離れたらアラートを表示するという機能を実装したのですが、シュミレーター場ではバックグラウンドで動いているにも関わらず、実機に落とすと動きません。
21
+ 現在地を取得して、特定の位置から離れたらアラートを表示するという機能を実装したのですが、シュミレーター場ではバックグラウンドで動いているにも関わらず、実機に落とすと動きません。(アプリを開いてホーム画面に戻った状態で、データが更新されません。)