简体   繁体   English

如何使用 Google Firebase Cloud Functions 检查位置是否在服务器上的地图范围内

[英]How to check if locations are within map bounds on server with Google Firebase Cloud Functions

What I'm trying to do:我想做什么:

  • Function on Firebase Functions creates geocodes an address Function on Firebase Functions 创建地址地理编码
  • There are a set of results that I want to verify are within the bounds of the map我想验证一组结果是否在地图范围内

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.

相关问题 如何在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase? Google Cloud Functions Firebase 错误 默认的 Firebase 应用已经存在 - Google Cloud Functions Firebase Error The default Firebase app already exists Map 自定义谷歌云功能 DNS - Map Google Cloud Functions with Custom DNS 如何为 firebase 云功能设置 vpc 连接器? - How to setup vpc connector for firebase cloud functions? firebase 的谷歌云函数中的“获取”函数是否每次都读取每个文档? - Are "get" functions in Google cloud functions for firebase reading each document everytime? Firebase/谷歌云功能基于时间的触发器防止并发执行 - Firebase/google cloud functions time based trigger prevent concurrent executions 如何使用云功能以使用 Firebase 云视觉 - How to use the Cloud Functions in order to use Firebase Cloud vision Cloud Functions for Firebase 超时 - Cloud Functions for Firebase timeout 如何使用 webpack 构建/编译 firebase 云函数 - How to build / compile firebase cloud functions with webpack 如何高效存储firebase云函数的Token? - How to store Token for firebase cloud functions efficiently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM