回答編集履歴

7

nil checkを修正

2019/11/22 06:58

投稿

hameji
hameji

スコア1380

test CHANGED
@@ -150,9 +150,9 @@
150
150
 
151
151
  // double型からString型へ、桁数指定とともに
152
152
 
153
- self.latitudeLabel.text = String(format: "%.4f", self.currentLatitude)
153
+ self.latitudeLabel.text = String(format: "%.4f", latitude)
154
154
 
155
- self.longitudeLabel.text = String(format: "%.4f", self.currentLongitude)
155
+ self.longitudeLabel.text = String(format: "%.4f", longitude)
156
156
 
157
157
  }
158
158
 

6

nil ckeck追加

2019/11/22 06:58

投稿

hameji
hameji

スコア1380

test CHANGED
@@ -144,11 +144,17 @@
144
144
 
145
145
  func updateUILabeltext() {
146
146
 
147
- // double型からString型へ、桁数指定とともに
147
+ // 自分もnil check忘れてました。
148
148
 
149
- self.latitudeLabel.text = String(format: "%.4f", self.currentLatitude)
149
+ if let latitude = self.currentLatitude, let longitude = self.currentLongitude {
150
150
 
151
+ // double型からString型へ、桁数指定とともに
152
+
153
+ self.latitudeLabel.text = String(format: "%.4f", self.currentLatitude)
154
+
151
- self.longitudeLabel.text = String(format: "%.4f", self.currentLongitude)
155
+ self.longitudeLabel.text = String(format: "%.4f", self.currentLongitude)
156
+
157
+ }
152
158
 
153
159
  }
154
160
 

5

間違い修正

2019/11/22 06:57

投稿

hameji
hameji

スコア1380

test CHANGED
@@ -84,7 +84,7 @@
84
84
 
85
85
  func post() {
86
86
 
87
- // nilチェックし、取得前ならそもそも送信しない
87
+ // nilチェックし、gps取得前ならそもそも送信しない
88
88
 
89
89
  if let latitude = self.currentLatitude, let longitude = self.currentLongitude {
90
90
 
@@ -92,7 +92,7 @@
92
92
 
93
93
  let postData = ["userLatitude": latitude, "userLongitude": longitude]
94
94
 
95
- ref.child("users").childByAutoId().setValue(post)
95
+ ref.child("users").childByAutoId().setValue(postData)
96
96
 
97
97
  } else {
98
98
 

4

微修正

2019/11/22 06:54

投稿

hameji
hameji

スコア1380

test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  ちなみに、LabelのtextプロパティはString型になりますからね。
20
20
 
21
- 結果からすると、Labelと出力されるはず???
21
+ データベースの結果からすると、Labelと出力されるはず???
22
22
 
23
23
 
24
24
 
@@ -70,7 +70,7 @@
70
70
 
71
71
  @IBOutlet weak var longitudeLabel: UILabel! // なんであるかわかりやすいようにLabel付けました
72
72
 
73
- var currentLatitude: Double? = nil // この行追加、取得前はnil(何もない)です
73
+ var currentLatitude: Double? = nil // この行追加、GPS取得前はnil(何もない)です
74
74
 
75
75
  var currentLongitude: Double? = nil // この行追加
76
76
 

3

大幅修正

2019/11/22 04:56

投稿

hameji
hameji

スコア1380

test CHANGED
@@ -1,8 +1,6 @@
1
1
  ```Swift
2
2
 
3
3
  func post() {
4
-
5
-
6
4
 
7
5
  ```
8
6
 
@@ -14,20 +12,148 @@
14
12
 
15
13
  print("latitude:", latitude.text, "longitude:", longitude.text)
16
14
 
15
+ ```
17
16
 
17
+ を入力し、送信する前の値を確認してみてはどうですか?
18
+
19
+ ちなみに、LabelのtextプロパティはString型になりますからね。
20
+
21
+ 結果からすると、Labelと出力されるはず???
22
+
23
+
24
+
25
+ ちょっと考えたんですが、
26
+
27
+ 気になるのはlocationManagerがきちんと動いているかですね、
28
+
29
+ 座標はprintでコンソールに表示されるんですよね?
30
+
31
+ そしてから、postを押しましたか?
32
+
33
+
34
+
35
+ locationManagerで座標が取得される前の状態でPostを押したら、
36
+
37
+ おそらくstoryboardで設定されている文字(推測するに"Label"?)が送信されます。
38
+
39
+
40
+
41
+ なので、プログラムはきちんと書かれている通りに動いていると思います。
42
+
43
+ 書き方に問題があるということになります。
44
+
45
+
46
+
47
+ 通常なら、GPSの座標を保存する入れものを別に用意し、それでいろいろやります。
48
+
49
+ 例えば、
50
+
51
+ ```Swift
52
+
53
+ import UIKit
54
+
55
+ import Firebase
56
+
57
+ import MapKit
58
+
59
+ import CoreLocation
60
+
61
+
62
+
63
+ class ViewController: UIViewController, CLLocationManagerDelegate {
64
+
65
+
66
+
67
+ @IBOutlet weak var mapView: MKMapView!
68
+
69
+ @IBOutlet weak var latitudeLabel: UILabel! // なんであるかわかりやすいようにLabel付けました(Double型の数値でないことわかるように)
70
+
71
+ @IBOutlet weak var longitudeLabel: UILabel! // なんであるかわかりやすいようにLabel付けました
72
+
73
+ var currentLatitude: Double? = nil // この行追加、取得前はnil(何もない)です
74
+
75
+ var currentLongitude: Double? = nil // この行追加
76
+
77
+
78
+
79
+ // ~~間省略
80
+
81
+
82
+
83
+ //データ書き込み
84
+
85
+ func post() {
86
+
87
+ // nilチェックし、取得前ならそもそも送信しない
88
+
89
+ if let latitude = self.currentLatitude, let longitude = self.currentLongitude {
90
+
91
+ // gps座標ありなので、送信(nilでない値=latitude, longitudeを使う)
92
+
93
+ let postData = ["userLatitude": latitude, "userLongitude": longitude]
94
+
95
+ ref.child("users").childByAutoId().setValue(post)
96
+
97
+ } else {
98
+
99
+ // nilの状態(=まだ取得されてない)なので、アラートなどでもどうぞ
100
+
101
+ }
102
+
103
+ }
104
+
105
+
106
+
107
+ // ~~間省略
108
+
109
+
110
+
111
+ func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
112
+
113
+ // ここも変更しました。 nilをまだよく理解していないみたいですね。
114
+
115
+ guard let newLocation = locations.last else {
116
+
117
+ return
118
+
119
+ }
120
+
121
+ // 以降はnilを除外した、newLocationを用います。
122
+
123
+ // 地図の表示変更
124
+
125
+ let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
126
+
127
+ let region = MKCoordinateRegion(center: newLocation.coordinate, span: span)
128
+
129
+ mapView.region = region
130
+
131
+ // 変数にセット
132
+
133
+ self.currentLatitude = newLocation.coordinate.latitude
134
+
135
+ self.currentLongitude = newLocation.coordinate.longitude
136
+
137
+ // UILabel更新, メンバ変数currentXXXを使うので、引数用いず。
138
+
139
+ updateUILabeltext()
140
+
141
+ }
142
+
143
+
144
+
145
+ func updateUILabeltext() {
146
+
147
+ // double型からString型へ、桁数指定とともに
148
+
149
+ self.latitudeLabel.text = String(format: "%.4f", self.currentLatitude)
150
+
151
+ self.longitudeLabel.text = String(format: "%.4f", self.currentLongitude)
152
+
153
+ }
18
154
 
19
155
  ```
20
156
 
21
157
 
22
158
 
23
- を入力し、送信する前値を確認してみてはどうですか?
159
+ こんなどうでしょう。
24
-
25
- ちなみに、LabelのtextプロパティはString型になりますからね。
26
-
27
-
28
-
29
- 結果からすると、Labelと出力されるはず???
30
-
31
- ちなみに気になったのは、locationManagerが動いているかですが、
32
-
33
- printで座標はコンソールに表示されるんですよね?

2

追記

2019/11/22 04:54

投稿

hameji
hameji

スコア1380

test CHANGED
@@ -27,3 +27,7 @@
27
27
 
28
28
 
29
29
  結果からすると、Labelと出力されるはず???
30
+
31
+ ちなみに気になったのは、locationManagerが動いているかですが、
32
+
33
+ printで座標はコンソールに表示されるんですよね?

1

微修正

2019/11/22 04:29

投稿

hameji
hameji

スコア1380

test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  を入力し、送信する前の値を確認してみてはどうですか?
24
24
 
25
- ちなみに、textFieldのtextプロパティはString型になりますからね。
25
+ ちなみに、LabelのtextプロパティはString型になりますからね。
26
26
 
27
27
 
28
28