のタイプ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
}
を表示してください。きみの。コード。 – matt
コード – NikaE
を追加しました。これは 'NSNumber'、' NSString'、 'NSDictionary'、および' NSArray'の形式で保存する必要があります。 '[{"経度 ":xxxx、" latitude ":xxxx}]'に保存します。 –