Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save GeoPoint in Firebase Cloud Firestore?

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?

like image 666
ProblemsOfSumit Avatar asked Oct 06 '17 10:10

ProblemsOfSumit


People also ask

What is the GeoPoint type in firebase?

An immutable object representing a geo point in Firestore. The geo point is represented as latitude/longitude pair. Latitude values are in the range of [-90, 90]. Longitude values are in the range of [-180, 180].

Can you store objects in firestore?

Cloud Firestore is optimized for storing large collections of small documents. All documents must be stored in collections. Documents can contain subcollections and nested objects, both of which can include primitive fields like strings or complex objects like lists.

What is a GeoPoint?

GeoPoint is a data meaning representing a point on Earth. This processor creates data in the (longitude,latitude) form.


2 Answers

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

{   location: new firebase.firestore.GeoPoint(latitude, longitude) } 
like image 149
Scarygami Avatar answered Oct 05 '22 23:10

Scarygami


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) } 
like image 31
Lal Avatar answered Oct 05 '22 22:10

Lal