lonelearner
lonelearner

Reputation: 1837

How to add or save from web form (Javascript) entry as GeoPoint type in Firebase Firestore

Hi i have a web form which collect the following:

-cafe name -latitude -longitude of its location.

Currently, the lat and long is saved as map format instead of GeoPoint in Firestore. Hence i would like to seek your advice on how it could be saved as GeoPoint format/data type.

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

I tried all the following but it does not works.

   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)

Upvotes: 0

Views: 1058

Answers (2)

Simon Klimek
Simon Klimek

Reputation: 352

you're missing correct referencing to your firebase app please see Vue.js Firestore save geopoint

the point you're missing is firebase import you have to import

import { firebase } from '@firebase/app';

the syntax you're using is correct

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

Upvotes: 1

Babis.amas
Babis.amas

Reputation: 471

If I'm right you need to send an array of coordinates to your firestore database so before the addition try this and then pass the array av value to your object's coordinates key.

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

Upvotes: 2

Related Questions