在Firestore中保存CLLocationCoordinate2D数组

问题描述 投票:-1回答:1

我正在尝试将CLLocationCoordinate2Darray保存到Firestore中,但我收到错误:

'FIRE InvalidArgumentException',原因:'不支持的类型:NSConcreteValue'

我真的不知道如何执行此操作。

在代码的顶部我是这样的:

var locations: [CLLocationCoordinate2D] = []

然后在“保存”按钮中。我有这个代码:

//Document Reference properties
        var _: DocumentReference = self.db
            //Collection into Subcollection & Documents
            .collection("rastad").document(authentication!)
            .collection("promenad").addDocument(data:
                //Insert first values
                ["Dog": txtfield_dog.text!,
                 "Person": txtfield_person.text!,
                 "What": txtfield_what.text!,
                 "Date": txtfield_Datum.text!,
                 "Time": txtfield_Time.text!,
                 "Timer": txtfield_timer.text!,
                 "Kilometers": txtfield_km.text!,
                 "Locations": locations
                ], completion: { (err) in
                    if err != nil
                    {
                        print("Error adding info-details")
                        return
                    }
                    else
                    {
                        print("Succeded!")
    }

那么,我该如何解决这个问题呢?如何将CLLocationCoordinate2D数组插入Firestore?而且,我怎么能在以后检索它呢?我真的很感激任何帮助!谢谢!

编辑:也许这个错误对你来说很清楚,但是很好。这对我来说并不清楚,这就是我当然要问这个问题的原因。如果我知道答案,我就不会回答这个问题。而不是不喜欢并离开主题,你至少可以发表评论并告诉我不喜欢的原因。所以,我猜我的问题会成立。

ios swift google-cloud-firestore cllocation cllocationcoordinate2d
1个回答
0
投票

为了打破它,Firebase不知道CLLocationCoordinate2D对象是什么。但它确实知道数组是什么,它确实知道字符串,整数和双精度。因此,您必须将您的位置数组解析为可以理解的位置。您可以将值存储在Firestore中,因为坐标实际上只是一对双精度值。您可以通过多种方式执行此操作,但最简单的方法是分割位置数组,一个用于纬度值,另一个用于经度。例如,您可以使用let latitudes = locations.map({ $0.latitude })和相同的经度。然后在Firestore中,您将其更新为:

                 "Person": txtfield_person.text!,
                 "What": txtfield_what.text!,
                 "Date": txtfield_Datum.text!,
                 "Time": txtfield_Time.text!,
                 "Timer": txtfield_timer.text!,
                 "Kilometers": txtfield_km.text!,
                 "LocationLatitudes": latitudes,
                 "LocationLongitudes": longitude,
                ]

应用反向逻辑进行检索。您将需要迭代lat和long数组并组合成一个CLLocationCoordinate2D对象数组。

© www.soinside.com 2019 - 2024. All rights reserved.