How to save GeoPoint in Firebase Cloud Firestore?

生来就可爱ヽ(ⅴ<●) 提交于 2020-03-17 04:44:11

问题


in the database UI I can create a field with the type geopoint. After providing values, it looks something like this:

location: [1° N, 1° E]

I'm trying to save this to the DB via client side SDK (web). Based on Twitter Feedback I tried GeoJSON, Coords Array and Coords Object:

location: [1, 2];
location: {
    latitude: 1,
    longitude: 2,
};
location: {
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [125.6, 10.1]
    }
}

None of which resulted in the geopoint type in the database. They're just saved as Objects/Arrays.

How to save GeoPoint in Firebase Cloud Firestore via the Web SDK?


回答1:


The Firestore SDKs include a GeoPoint class, so you would have to save locations like this:

{
  location: new firebase.firestore.GeoPoint(latitude, longitude)
}



回答2:


Simply use:

import 'package:cloud_firestore/cloud_firestore.dart';
{
  location:GeoPoint(latitude, longitude)
}



回答3:


I struggled to find out the right import/require. This is what worked for me !!

const firebaseAdmin = require('firebase-admin');
//other code ....

//assigning value to myLocation attribute
let newObject = {
    otherAttribute:"otherAttributeValue",
    myLocation: new firebaseAdmin.firestore.GeoPoint(latitude,longitude)
}


来源:https://stackoverflow.com/questions/46603691/how-to-save-geopoint-in-firebase-cloud-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!