How to Add/Save Geopoint variable to Firestore (Javascript)

2,717 views
Skip to first unread message

Glenn Tan

unread,
May 29, 2020, 11:31:33 AM5/29/20
to Firebase Google Group
Dear Sir/Madam

I am writing a web app using Javascript. I would like to seek advise on how to add/save a variable of type Geopoint to firestore? By default, variable could be add to firebase by add(var_A: value). However, a geopoint variable has two variable: latitude and longitude. So would like to enquire on how to write it. In my example below, I have a variable "latandlongofcafe" which is a geopoint.

form.addEventListener('submit',(e)=>{
 e
.preventDefault();
 db
.collection('cafe').add({
  city
: form.city.value,
  name
: form.name.value,
  latandlongofcafe
: form.latitude.value, form.longitude.value //need help to advice on how to save lat and long
 
});
});


Kato Richardson

unread,
May 29, 2020, 5:27:22 PM5/29/20
to Firebase Google Group
Hi Glenn,

Firestore has a Geopoint data type. You can also use an array [<lat>, <long>] or an object {lat: <lat>, long: <long>}. See this video for an overview of data structures.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/7f1dfe24-d34f-48a2-af9a-242210bbb1bc%40googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Glenn Tan

unread,
May 30, 2020, 10:45:14 AM5/30/20
to Firebase Google Group
Hi Kato

Greatly appreciate your help. I have gone through the video and have tried the following:

 coordinates: {Latitude:form.lat.value, Longtitude:form.long.value}

The coordinates variable is saved as map format (data type) instead as of geopoint format.
Would you be able to advice/show the code on how to save as geopoint format?

Your Sincerely
Glenn

On Saturday, 30 May 2020 05:27:22 UTC+8, Kato Richardson wrote:
Hi Glenn,

Firestore has a Geopoint data type. You can also use an array [<lat>, <long>] or an object {lat: <lat>, long: <long>}. See this video for an overview of data structures.

☼, Kato

On Fri, May 29, 2020 at 8:31 AM Glenn Tan <rawis...@gmail.com> wrote:
Dear Sir/Madam

I am writing a web app using Javascript. I would like to seek advise on how to add/save a variable of type Geopoint to firestore? By default, variable could be add to firebase by add(var_A: value). However, a geopoint variable has two variable: latitude and longitude. So would like to enquire on how to write it. In my example below, I have a variable "latandlongofcafe" which is a geopoint.

form.addEventListener('submit',(e)=>{
 e
.preventDefault();
 db
.collection('cafe').add({
  city
: form.city.value,
  name
: form.name.value,
  latandlongofcafe
: form.latitude.value, form.longitude.value //need help to advice on how to save lat and long
 
});
});


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.

Kato Richardson

unread,
May 30, 2020, 12:04:31 PM5/30/20
to Firebase Google Group

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/1d42f2a3-0702-4559-94a5-bc15489ce743%40googlegroups.com.

Glenn Tan

unread,
May 31, 2020, 12:18:28 AM5/31/20
to fireba...@googlegroups.com
Hi Kato

Sorry just to check the public constructor which you share is for which platform?

I am using javascript (web). Would greatly appreciate if you can show in code (javascript) how to save geopoint from web to firebase firestore. 


Kato Richardson

unread,
May 31, 2020, 3:11:11 PM5/31/20
to Firebase Google Group
According to the doc you just linked: new GeoPoint ( latitude :  number ,  longitude :  number ) GeoPoint

So this:  new GeoPoint( 123, 456 );

Glenn Tan

unread,
Jul 5, 2020, 11:07:48 AM7/5/20
to Firebase Google Group
Hi Kato

Thank you for advising.  Sorry for the late reply, was busy with other works.
 
To update, as per your advice, i have understood how to use GeoPoint and implemented it successfully.  

For others in the community who may face similar issue in the future, this is what my code looks like:

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

I would like to extend my thanks to Kato, greatly appreciate your help.

Best Regard
Glenn Tan

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages