2017-03-01 14 views
-1

のタイプCLLocationの(のsetValue :)できませんストアオブジェクトFirebaseにlet myLocations[CLLocation]を保存しようとしますが、エラーを取得:エラー:理由:「locations.0

reason: '(setValue:) Cannot store object of type CLLocation at locations.0. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.

任意の助けを?

コード:

var myLocations: [CLLocation] = [] 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     myLocations.append(locations[0] as CLLocation) 

     let spanX = 0.007 
     let spanY = 0.007 

     var location = CLLocation() 
     for L in myLocations { 
      location = L 
     } 
     let newRegion = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpanMake(spanX, spanY)) 
     theMap.setRegion(newRegion, animated: true) 

     if (myLocations.count > 3){ 
      let sourceIndex = myLocations.count - 1 
      let destinationIndex = myLocations.count - 4 
      let c1 = myLocations[sourceIndex].coordinate 
      let c2 = myLocations[destinationIndex].coordinate 
      var a = [c1, c2] 
      let polyline = MKPolyline(coordinates: &a, count: a.count) 
      polyline.title = "polyline" 
      theMap.add(polyline) 
     } 

     if startLocation == nil { 
      startLocation = locations.first as CLLocation! 
     } else { 
      let lastDistance = lastLocation.distance(from: locations.last as CLLocation!) //In Meter 
      distanceTraveled += lastDistance * 0.000621371192 //In Miles 
      //1 Meter = 0.000621371192 Miles 
      //1 Mile = 1609.344 Meters 
      distanceLabel_String = String(format: "%.2f mi", distanceTraveled) 
      distanceLabel.text = distanceLabel_String 
      let altitude = lastLocation.altitude // In Meters 
      let altitudeInFeets = altitude/0.3048 //In Feets 
      arrayOfAltitude.append(altitudeInFeets) 
      let maxAltitude = arrayOfAltitude.max() 
      altitudeLabel_String = String(format: "%.2f ft", maxAltitude!) 
      altitudeLabel.text = String(format: "%.2f ft", altitudeInFeets) 
     } 

     lastLocation = locations.last as CLLocation! 

    } 




func saveLocations() { 

    let locations = myLocations 

    let trailInfo: Dictionary<String, Any> = [ "locations" : locations,] 


     let databaseRef = FIRDatabase.database().reference() 
     databaseRef.child("Trails").childByAutoId().setValue(trailInfo) 
    } 

@IBAction func doneActivityHit(_ sender: UIButton) { 
    saveLocations() 
    self.view.removeFromSuperview() 
    myLocations.removeAll() 
    distanceTraveled = 0 
} 
+1

を表示してください。きみの。コード。 – matt

+0

コード – NikaE

+1

を追加しました。これは 'NSNumber'、' NSString'、 'NSDictionary'、および' NSArray'の形式で保存する必要があります。 '[{"経度 ":xxxx、" latitude ":xxxx}]'に保存します。 –

答えて

1

あなたのエラーメッセージは明確である:

reason: '(setValue:) Cannot store object of type CLLocation at locations.0. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.

setValue:docsは同じことを言う:

Data types that can be set are:

NSString – @“Hello World”

NSNumber (also includes boolean) – @YES, @43, @4.333

NSDictionary – @{@“key”: @“value”, @“nested”: @{@“another”: @“value”} }

NSArray

CLLocationは、これらのいずれかのではありません。 setValueを使用してCLLocationを保存することはできません。

通常は、各CLLocationを緯度と経度に分割します。数字(NSNumberがリストに表示されていることに注意してください)を辞書として表示します(NSDictionaryがリストに表示されます)。だから、場所theLocation与え、あなたが言う:

let dict = ["Latitude": theLocation.coordinate.latitude, "Longitude": theLocation.coordinate.longitude] 

あなたが保存(と場所を取得し、再構築するプロセスを逆に)したい場所ごとにそれを行うことができます。

+0

私はこのことを理解しています。「setValueを使ってCLLocationを保存することはできません。任意のソリューションですか? – NikaE

+1

編集された答えを読んでください...誰でもこれを落としたのはちょうど馬鹿です、無視してください。 – matt

関連する問題