'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?
Solution 1:[1]
The Firestore SDKs include a GeoPoint class
, so you would have to save locations like this:
{
location: new firebase.firestore.GeoPoint(latitude, longitude)
}
Solution 2:[2]
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)
}
Solution 3:[3]
Simply use:
import 'package:cloud_firestore/cloud_firestore.dart';
{
location:GeoPoint(latitude, longitude)
}
Solution 4:[4]
VERSION 9 Firestore
import { GeoPoint} from "firebase/firestore";
const dataDocument = {
tienda: new GeoPoint(latitude: number, longitude: number),
}
Solution 5:[5]
For PHP using "google/cloud-firestore" package
you have to use this class Google\Cloud\Core\GeoPoint
example:
<?php
$geoPoint = new \Google\Cloud\Core\GeoPoint($latitude , $longitude);
$data = ['geopoint' => $geoPoint];
Solution 6:[6]
GeoFirePoint geoFirePoint = geo.point(latitude: lat, longitude: lng);
_firestore
.collection('locations')
.add({'name': 'random name', 'position': geoFirePoint.data}).then((_) {
print('added ${geoFirePoint.hash} successfully');
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Scarygami |
Solution 2 | Lal |
Solution 3 | Hasan A Yousef |
Solution 4 | Andres Diaz Lopez |
Solution 5 | Dharman |
Solution 6 | gsm |