本文介绍了如何将坐标数组写入Cloud Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将坐标数组作为GeoPoint数据类型写入Cloud Firestore?

How can I write an array of coordinates to the Cloud Firestore as a GeoPoint datatype?

我确实有一个带有坐标的arraylist<latlng> points,我需要将这些坐标写入Firestore:

I do have an arraylist<latlng> points with coordinates and I need to write those coordinates to Firestore:

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;

    mLatLng = new
LatLng(location.getLatitude(),location.getLongitude());
    mMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));

    mMap.animateCamera(CameraUpdateFactory.zoomTo(20));

    if (mDraw) {
        points.add(mLatLng); //added
        drawPolyLine();
    }


}

private void drawPolyLine(){
    mMap.clear();

    PolylineOptions options = new
PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int i = 0; i < points.size(); i++) {
        LatLng point = points.get(i);
        options.add(point);
    }

    line = mMap.addPolyline(options); //add Polyline
}

我尝试docRef.update("Test", FieldValue.arrayUnion(points));docRef.set(points);都导致错误Nested arrays not supportedrequired map String or related POJO respectively.

What I tried docRef.update("Test", FieldValue.arrayUnion(points)); and docRef.set(points); both resulted in errors Nested arrays not supported and required map String or related POJO respectively.

推荐答案

通过 FieldValue.arrayUnion 作为更新( )方法,如以下代码行所示:

When passing FieldValue.arrayUnion as the second argument to the update() method like in the the following line of code:

docRef.update("Test", FieldValue.arrayUnion(points));

这意味着您要告诉Firebase您想用List更新类型为array的文档中的属性,但这实际上是不可能的,因此出现此错误:

It means that you are telling Firebase that you want to update a property within a document which is of type array with a List, which is actually not possible, hence this error:

Nested arrays not supported

如果您的文档包含嵌套数组,请注意,当前无法进行常规更新.相反,您可以做的是获取整个文档,然后调用DocumentSnapshot对象上的"nofollow noreferrer"> getData().返回的对象类型是Map<String, Object>.遍历地图,更新所需的值,然后将文档写回到数据库中.

If you have documents that contain nested arrays, please note that a regular update is currently not possible. What can you do instead is to get the entire document and call getData() on the DocumentSnapshot object. The type of object that is returned is a Map<String, Object>. Iterate through the map, update the desired value and write the document back in the database.

这篇关于如何将坐标数组写入Cloud Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 05:51