首页 > 解决方案 > 如何在 Firebase Firestore 中将 Web 表单(Javascript)条目添加或保存为 GeoPoint 类型

问题描述

嗨,我有一个收集以下内容的网络表单:

-cafe 名称 -latitude -其位置的经度。

目前,经纬度在 Firestore 中保存为地图格式,而不是 GeoPoint。因此,我想就如何将其保存为 GeoPoint 格式/数据类型征求您的意见。

form.addEventListener('submit',(e)=>{
  e.preventDefault();
  db.collection('sgcafe').add ({
    name: form.name.value,
    category: form.category.value,
    coordinates: {Latitude:form.lat.value,Longtitude:form.long.value} //save as map not Geopoint

我尝试了以下所有方法,但它不起作用。

   coordinates: new firebase.firestore.GeoPoint(form.lat.value, form.long.value)
   coordinates: firebase.firestore.GeoPoint(form.lat.value, form.long.value)
   coordinates: GeoPoint(form.lat.value, form.long.value)

标签: javascriptfirebasegoogle-maps-api-3google-cloud-firestore

解决方案


如果我是对的,您需要将坐标数组发送到您的 firestore 数据库,因此在添加之前尝试此操作,然后将数组 av 值传递给对象的坐标键。

let coords = []
coords.push(new firebase.firestore.GeoPoint(form.lat.value, form.long.value))

推荐阅读