在Firestore中保存CLLocationCooridnate2D数组 [英] Save a CLLocationCooridnate2D array in Firestore

查看:127
本文介绍了在Firestore中保存CLLocationCooridnate2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存一个 CLLocationCoordinate2D将数组放入Firestore,但出现错误:

I'm trying to save a CLLocationCoordinate2Darray into Firestore, but I'm getting error:

"FIRInvalidArgumentException",原因:不受支持的类型:NSConcreteValue"

'FIRInvalidArgumentException', reason: 'Unsupported type: NSConcreteValue'

我真的不知道该怎么做.

And I don't really know how I can perform this.

在代码顶部,我具有以下内容:

In the top of the code I've this:

var locations: [CLLocationCoordinate2D] = []

然后点击保存"按钮.我有这段代码:

And down in the 'save' button. I've this code:

//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?而且,以后我怎么才能找回它?我将非常感谢您的帮助!谢谢!


So, how can I solve this issue? How can I insert the CLLocationCoordinate2D array into Firestore? And also, how can I later on retrieve it? I would really appreciate any help! Thanks!

也许这个错误对您来说很清楚,但是很好.对我来说还不清楚,这就是我问当然问题的原因.如果我知道答案,我不会回答这个问题.而且,除了不喜欢并退出话题之外,您至少可以发表评论并告诉我不喜欢的原因.所以,我想我的问题仍然存在.

Maybe this error is clear for you, but well. It's not clear for me and thats the reason why I'm asking the question of course. If I'd know the answer I would not answer this question. And instead of disliking and leave the thread, you can at least leave a comment and tell me the reason for the dislike. So, I guess my question will stand.

推荐答案

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

To break it down, Firebase doesn't know what a CLLocationCoordinate2D object is. But it does know what an array is, and it does know what strings and integers and doubles are. So you will have to parse your array of locations into something it can understand. You can store the value in Firestore since a coordinate is really just a pair of double values. You can do this a number of ways, but the easiest is to probably split the locations array, one for the latitude values and one for longitude. For example, you can use let latitudes = locations.map({ $0.latitude }) and the same for longitudes. Then in Firestore, you would update it to something like:

                 "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&长数组,并合并为CLLocationCoordinate2D个对象的单个数组.

Apply reverse logic for retrieving. You will need to iterate over the lat & long arrays and combine into a single array of CLLocationCoordinate2D objects.

这篇关于在Firestore中保存CLLocationCooridnate2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆