回答編集履歴
7
nil checkを修正
    
        answer	
    CHANGED
    
    | @@ -74,8 +74,8 @@ | |
| 74 74 | 
             
                    // 自分もnil check忘れてました。
         | 
| 75 75 | 
             
                    if let latitude = self.currentLatitude, let longitude = self.currentLongitude {
         | 
| 76 76 | 
             
                        // double型からString型へ、桁数指定とともに
         | 
| 77 | 
            -
                        self.latitudeLabel.text = String(format: "%.4f",  | 
| 77 | 
            +
                        self.latitudeLabel.text = String(format: "%.4f", latitude)
         | 
| 78 | 
            -
                        self.longitudeLabel.text = String(format: "%.4f",  | 
| 78 | 
            +
                        self.longitudeLabel.text = String(format: "%.4f", longitude)
         | 
| 79 79 | 
             
                    }
         | 
| 80 80 | 
             
                }
         | 
| 81 81 | 
             
            ```
         | 
6
nil ckeck追加
    
        answer	
    CHANGED
    
    | @@ -71,9 +71,12 @@ | |
| 71 71 | 
             
                }
         | 
| 72 72 |  | 
| 73 73 | 
             
                func updateUILabeltext() {
         | 
| 74 | 
            +
                    // 自分もnil check忘れてました。
         | 
| 75 | 
            +
                    if let latitude = self.currentLatitude, let longitude = self.currentLongitude {
         | 
| 74 | 
            -
             | 
| 76 | 
            +
                        // double型からString型へ、桁数指定とともに
         | 
| 75 | 
            -
             | 
| 77 | 
            +
                        self.latitudeLabel.text = String(format: "%.4f", self.currentLatitude)
         | 
| 76 | 
            -
             | 
| 78 | 
            +
                        self.longitudeLabel.text = String(format: "%.4f", self.currentLongitude)
         | 
| 79 | 
            +
                    }
         | 
| 77 80 | 
             
                }
         | 
| 78 81 | 
             
            ```
         | 
| 79 82 |  | 
5
間違い修正
    
        answer	
    CHANGED
    
    | @@ -41,11 +41,11 @@ | |
| 41 41 |  | 
| 42 42 | 
             
                //データ書き込み
         | 
| 43 43 | 
             
                func post() {
         | 
| 44 | 
            -
                    // nilチェックし、取得前ならそもそも送信しない
         | 
| 44 | 
            +
                    // nilチェックし、gps取得前ならそもそも送信しない
         | 
| 45 45 | 
             
                    if let latitude = self.currentLatitude, let longitude = self.currentLongitude {
         | 
| 46 46 | 
             
                        // gps座標ありなので、送信(nilでない値=latitude, longitudeを使う)
         | 
| 47 47 | 
             
                        let postData = ["userLatitude": latitude, "userLongitude": longitude]
         | 
| 48 | 
            -
                        ref.child("users").childByAutoId().setValue( | 
| 48 | 
            +
                        ref.child("users").childByAutoId().setValue(postData)
         | 
| 49 49 | 
             
                    } else {
         | 
| 50 50 | 
             
                        // nilの状態(=まだ取得されてない)なので、アラートなどでもどうぞ
         | 
| 51 51 | 
             
                    }
         | 
4
微修正
    
        answer	
    CHANGED
    
    | @@ -8,7 +8,7 @@ | |
| 8 8 | 
             
            ```
         | 
| 9 9 | 
             
            を入力し、送信する前の値を確認してみてはどうですか?
         | 
| 10 10 | 
             
            ちなみに、LabelのtextプロパティはString型になりますからね。
         | 
| 11 | 
            -
            結果からすると、Labelと出力されるはず???
         | 
| 11 | 
            +
            データベースの結果からすると、Labelと出力されるはず???
         | 
| 12 12 |  | 
| 13 13 | 
             
            ちょっと考えたんですが、
         | 
| 14 14 | 
             
            気になるのはlocationManagerがきちんと動いているかですね、
         | 
| @@ -34,7 +34,7 @@ | |
| 34 34 | 
             
                @IBOutlet weak var mapView: MKMapView!
         | 
| 35 35 | 
             
                @IBOutlet weak var latitudeLabel: UILabel!  // なんであるかわかりやすいようにLabel付けました(Double型の数値でないことわかるように)
         | 
| 36 36 | 
             
                @IBOutlet weak var longitudeLabel: UILabel!  // なんであるかわかりやすいようにLabel付けました
         | 
| 37 | 
            -
                var currentLatitude: Double? = nil  // この行追加、取得前はnil(何もない)です
         | 
| 37 | 
            +
                var currentLatitude: Double? = nil  // この行追加、GPS取得前はnil(何もない)です
         | 
| 38 38 | 
             
                var currentLongitude: Double? = nil // この行追加
         | 
| 39 39 |  | 
| 40 40 | 
             
            // ~~間省略
         | 
3
大幅修正
    
        answer	
    CHANGED
    
    | @@ -1,17 +1,80 @@ | |
| 1 1 | 
             
            ```Swift
         | 
| 2 2 | 
             
            func post() {
         | 
| 3 | 
            -
             | 
| 4 3 | 
             
            ```
         | 
| 5 4 | 
             
            の次の行に
         | 
| 6 5 |  | 
| 7 6 | 
             
            ```Swift
         | 
| 8 7 | 
             
            print("latitude:", latitude.text, "longitude:", longitude.text)
         | 
| 9 | 
            -
             | 
| 10 8 | 
             
            ```
         | 
| 11 | 
            -
             | 
| 12 9 | 
             
            を入力し、送信する前の値を確認してみてはどうですか?
         | 
| 13 10 | 
             
            ちなみに、LabelのtextプロパティはString型になりますからね。
         | 
| 11 | 
            +
            結果からすると、Labelと出力されるはず???
         | 
| 14 12 |  | 
| 15 | 
            -
             | 
| 13 | 
            +
            ちょっと考えたんですが、
         | 
| 16 | 
            -
             | 
| 14 | 
            +
            気になるのはlocationManagerがきちんと動いているかですね、
         | 
| 17 | 
            -
             | 
| 15 | 
            +
            座標はprintでコンソールに表示されるんですよね?
         | 
| 16 | 
            +
            そしてから、postを押しましたか?
         | 
| 17 | 
            +
             | 
| 18 | 
            +
            locationManagerで座標が取得される前の状態でPostを押したら、
         | 
| 19 | 
            +
            おそらくstoryboardで設定されている文字(推測するに"Label"?)が送信されます。
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            なので、プログラムはきちんと書かれている通りに動いていると思います。
         | 
| 22 | 
            +
            書き方に問題があるということになります。
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            通常なら、GPSの座標を保存する入れものを別に用意し、それでいろいろやります。
         | 
| 25 | 
            +
            例えば、
         | 
| 26 | 
            +
            ```Swift
         | 
| 27 | 
            +
            import UIKit
         | 
| 28 | 
            +
            import Firebase
         | 
| 29 | 
            +
            import MapKit
         | 
| 30 | 
            +
            import CoreLocation
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            class ViewController: UIViewController, CLLocationManagerDelegate {
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                @IBOutlet weak var mapView: MKMapView!
         | 
| 35 | 
            +
                @IBOutlet weak var latitudeLabel: UILabel!  // なんであるかわかりやすいようにLabel付けました(Double型の数値でないことわかるように)
         | 
| 36 | 
            +
                @IBOutlet weak var longitudeLabel: UILabel!  // なんであるかわかりやすいようにLabel付けました
         | 
| 37 | 
            +
                var currentLatitude: Double? = nil  // この行追加、取得前はnil(何もない)です
         | 
| 38 | 
            +
                var currentLongitude: Double? = nil // この行追加
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            // ~~間省略
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                //データ書き込み
         | 
| 43 | 
            +
                func post() {
         | 
| 44 | 
            +
                    // nilチェックし、取得前ならそもそも送信しない
         | 
| 45 | 
            +
                    if let latitude = self.currentLatitude, let longitude = self.currentLongitude {
         | 
| 46 | 
            +
                        // gps座標ありなので、送信(nilでない値=latitude, longitudeを使う)
         | 
| 47 | 
            +
                        let postData = ["userLatitude": latitude, "userLongitude": longitude]
         | 
| 48 | 
            +
                        ref.child("users").childByAutoId().setValue(post)
         | 
| 49 | 
            +
                    } else {
         | 
| 50 | 
            +
                        // nilの状態(=まだ取得されてない)なので、アラートなどでもどうぞ
         | 
| 51 | 
            +
                    }
         | 
| 52 | 
            +
                }
         | 
| 53 | 
            +
             | 
| 54 | 
            +
            // ~~間省略
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
         | 
| 57 | 
            +
                    // ここも変更しました。 nilをまだよく理解していないみたいですね。
         | 
| 58 | 
            +
                    guard let newLocation = locations.last else {
         | 
| 59 | 
            +
                            return
         | 
| 60 | 
            +
                    }
         | 
| 61 | 
            +
                    // 以降はnilを除外した、newLocationを用います。
         | 
| 62 | 
            +
                    // 地図の表示変更
         | 
| 63 | 
            +
                    let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
         | 
| 64 | 
            +
                    let region = MKCoordinateRegion(center: newLocation.coordinate, span: span)
         | 
| 65 | 
            +
                    mapView.region = region
         | 
| 66 | 
            +
                    // 変数にセット
         | 
| 67 | 
            +
                    self.currentLatitude = newLocation.coordinate.latitude
         | 
| 68 | 
            +
                    self.currentLongitude = newLocation.coordinate.longitude
         | 
| 69 | 
            +
                    // UILabel更新, メンバ変数currentXXXを使うので、引数用いず。
         | 
| 70 | 
            +
                    updateUILabeltext()
         | 
| 71 | 
            +
                }
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                func updateUILabeltext() {
         | 
| 74 | 
            +
                    // double型からString型へ、桁数指定とともに
         | 
| 75 | 
            +
                    self.latitudeLabel.text = String(format: "%.4f", self.currentLatitude)
         | 
| 76 | 
            +
                    self.longitudeLabel.text = String(format: "%.4f", self.currentLongitude)
         | 
| 77 | 
            +
                }
         | 
| 78 | 
            +
            ```
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            こんなのでどうでしょう。
         | 
2
追記
    
        answer	
    CHANGED
    
    | @@ -12,4 +12,6 @@ | |
| 12 12 | 
             
            を入力し、送信する前の値を確認してみてはどうですか?
         | 
| 13 13 | 
             
            ちなみに、LabelのtextプロパティはString型になりますからね。
         | 
| 14 14 |  | 
| 15 | 
            -
            結果からすると、Labelと出力されるはず???
         | 
| 15 | 
            +
            結果からすると、Labelと出力されるはず???
         | 
| 16 | 
            +
            ちなみに気になったのは、locationManagerが動いているかですが、
         | 
| 17 | 
            +
            printで座標はコンソールに表示されるんですよね?
         | 
1
微修正
    
        answer	
    CHANGED
    
    | @@ -10,6 +10,6 @@ | |
| 10 10 | 
             
            ```
         | 
| 11 11 |  | 
| 12 12 | 
             
            を入力し、送信する前の値を確認してみてはどうですか?
         | 
| 13 | 
            -
            ちなみに、 | 
| 13 | 
            +
            ちなみに、LabelのtextプロパティはString型になりますからね。
         | 
| 14 14 |  | 
| 15 15 | 
             
            結果からすると、Labelと出力されるはず???
         | 
