LoginSignup
3
4

More than 3 years have passed since last update.

【firestore】 Cloud Functions for FirebaseからFire Storeへデータ追加する~位置情報(GeoPoint)編~

Posted at

今開発中のアプリの中でfirestoreにGeoPoint型のデータを追加する処理を書く機会があったので、忘備録も兼ねて記載。

Cloud FunctionsでFire StoreにGeoPoint型のデータ追加を行う

GeoPointクラスに関するFirebase公式ドキュメントはこちら
(日本語のリファレンスはまだないみたいですね...)

サンプルコード

今回はサンプルコードとして梅田駅の位置情報をfirestoreに追加するファンクションを書いてみます。

index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var fireStore = admin.firestore();

  exports.insertHotelRoom = functions.region('asia-northeast1').https.onRequest((request, response) => {
    var dataRef = fireStore.collection('Stations');
    var data ={
        Address:'大阪府大阪市北区梅田1-1-1',
        Area:'梅田',
        Location:  new admin.firestore.GeoPoint(34.421740, 135.295420),
        Name: 'UMEDA STATION'
    }
    // Add a new document with a generated id.
    dataRef.add({
        data
    }).then(ref => {
        console.log('Added document with ID: ', ref.id);
    }).catch(err =>{
        response.send('ERROR HAPPENED.'+err)
    });
  })

簡単ですが、以上になります。
シンプルにnew admin.firestore.GeoPoint(緯度,経度)でGeoPointの設定ができるみたいですね。
参考になれば幸いです。

参考にした記事

本記事を書くにあたって(というよりもCloud FunctionsからFire Storeを参照する処理について)以下の記事を参考にさせていただきました。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4