如何在 python 脚本中读取 firestore GeoPoint?

How to read firestore GeoPoint in python script?

我正在编写 Python 脚本来读取 Firebase Firestore 数据。不知何故 documentSnapshop.to_dict() 对我不起作用。所以,我参考https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/firestore/cloud-client/snippets.py#L103这个资源写了一个class

在我的示例中,城市的位置存储为 GeoPoint firestore 字段。我无法在字典中找到它。我们如何在上面提到的参考示例中提到的城市 class 中添加 GeoPoint 位置?

假设 GeoLocation 属性 被命名为 location,您应该像这样添加它:

class City(object):
    def __init__(self, name, state, country, capital=False, population=0,
                 regions=[], location):
        self.name = name
        self.state = state
        self.country = country
        self.capital = capital
        self.population = population
        self.regions = regions
        self.location = location

然后,当从数据库中获取数据时,您可以这样设置 location 属性:

source = db.collection(u'collection').document(u'docId').get().to_dict()
city = City(source[u'name'], source[u'state'], source[u'country'], source[u'country'], source[u'location'])

如果您想获取纬度或经度,可以使用以下代码:

city.location.latitude
city.location.longitude