ProblemsOfSumit
ProblemsOfSumit

Reputation: 21375

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?

Upvotes: 50

Views: 41313

Answers (7)

AndroidEngineX
AndroidEngineX

Reputation: 1481

In typescript, you can use below code for firebase-admin sdk,

import { GeoPoint } from "firebase-admin/firestore";

const geoPoint = new GeoPoint(latitude, longitude)

this is for "firebase-admin": "11.2.0" library version.

Also ensure that your, latitude must be within [-90, 90] inclusive while longitude must be within [-180, 180] inclusive. Otherwise firebase sdk will throw Value for argument \"longitude\" must be within [-180, 180] inclusive, but was: 230

In case of web SDK, simply update the import as follow

import { GeoPoint } from "firebase/firestore";

Upvotes: 2

Andres Diaz Lopez
Andres Diaz Lopez

Reputation: 612

VERSION 9 Firestore

import { GeoPoint} from "firebase/firestore";
const dataDocument = {
 tienda: new GeoPoint(latitude: number, longitude: number),
}

Upvotes: 4

gsm
gsm

Reputation: 2426

 GeoFirePoint geoFirePoint = geo.point(latitude: lat, longitude: lng);
    _firestore
        .collection('locations')
        .add({'name': 'random name', 'position': geoFirePoint.data}).then((_) {
      print('added ${geoFirePoint.hash} successfully');
    });

Upvotes: 0

mahmudy-91
mahmudy-91

Reputation: 81

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];

Upvotes: 0

Lal
Lal

Reputation: 146

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)
}

Upvotes: 9

Hasan A Yousef
Hasan A Yousef

Reputation: 24988

Simply use:

import 'package:cloud_firestore/cloud_firestore.dart';
{
  location:GeoPoint(latitude, longitude)
}

Upvotes: 5

Scarygami
Scarygami

Reputation: 15569

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

{
  location: new firebase.firestore.GeoPoint(latitude, longitude)
}

Upvotes: 104

Related Questions