[英]How do I get the server timestamp in Cloud Functions for Firebase?
[英]How to check if locations are within map bounds on server with Google Firebase Cloud Functions
我想做什么:
導入FB Functions和Admin,初始化
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
在測試期間處理 CORS
const cors = require('cors');
const corsHandler = cors({origin: true});
Google 地圖客戶端 - google-maps-services-js
const googleMapsClient = require('@google/maps').createClient({
key: 'xxxxxxx'
});
主要問題是我想查詢數據庫並讓后端檢查結果是否在地圖范圍內,過濾結果並做出響應。 我不想獲取所有結果並在前端過濾它們。
exports.myFunction = functions.https.onRequest((request, response) => {
// CORS Container - allow all request origins
corsHandler(request, response, () => {
// Retrieve listings data from DB
return admin.database().ref('/listings').once('value').then(snapshots => {
// Geocode an address.
return googleMapsClient.geocode({
address: '1600 Amphitheatre Parkway, Mountain View, CA'
}, function(err, response) {
if (!err) {
let res = response.json.results;
let lat = res[0]["geometry"]["location"]["lat"];
let lng = res[0]["geometry"]["location"]["lng"];
// Want to get the bounds here
// let bounds = ???
let results = [];
// Filter snapshot results
snapshots.forEach(snapshot => {
// Get snapshot's position
const position = snapshot.val().position;
// Check if snapshot position is within map's bounds
if (bounds.contains(position)) {
results.push(snapshot);
}
});
return response.status(200).send({
"data": bounds
});
}
});
}).catch(e => {
return response.status(422).send({
"data": e
});
});
});
});
獲取地圖的邊界並檢查位置對象是否在前端的邊界內很容易:
map.getBounds().contains(marker.getPosition());
努力通過 Firebase Functions 使它在后端運行。
可以試試
return response.status(200).send({
"data": results
});
反而
return response.status(200).send({
"data": bounds
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.