[英]How to check if locations are within map bounds on server with Google Firebase Cloud Functions
What I'm trying to do:我想做什么:
Import FB Functions and Admin, Initialize导入FB Functions和Admin,初始化
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
Handle CORS during testing在测试期间处理 CORS
const cors = require('cors');
const corsHandler = cors({origin: true});
Google Maps Clients - google-maps-services-js Google 地图客户端 - google-maps-services-js
const googleMapsClient = require('@google/maps').createClient({
key: 'xxxxxxx'
});
The main issue is that I want to query the db and have the backend check if the results are within the bounds of the map, filter the results down and respond.主要问题是我想查询数据库并让后端检查结果是否在地图范围内,过滤结果并做出响应。 I don't want to get all the results and filter them on the frontend.
我不想获取所有结果并在前端过滤它们。
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
});
});
});
});
It's easy to get the map's bounds and check if a location object is within bounds on the frontend:获取地图的边界并检查位置对象是否在前端的边界内很容易:
map.getBounds().contains(marker.getPosition());
Struggling to get this working on the backend with Firebase Functions.努力通过 Firebase Functions 使它在后端运行。
may be try可以试试
return response.status(200).send({
"data": results
});
instead反而
return response.status(200).send({
"data": bounds
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.