Firebase Cloud Functions 使用 @type/googlemaps 命名空间时遇到的问题:"ReferenceError: google is not defined"



我需要在我的云函数中使用Google Maps Api库。我用npm install --save @types/google-maps安装了命名空间,所以vsCode不会给我任何编译错误,但当云函数触发时,我得到了这个错误:

ReferenceError: google is not defined at exports.onZoneCreate.functions.database.ref.onCreate(/user_code/lib/index.js:93:20)

触发的功能是

export const onZoneCreate = functions.database
.ref('/Zones/Areas/{CAP}/{IDzona}')
.onCreate((snapshot, context) => {

const cap: string = context.params.CAP
const IDZona: string = context.params.IDzona
console.log('new zone (id Zona:' + IDZona + ') from CAP ' + cap)
const feature = snapshot.val();
const vettoreCoordinatePoligono = feature.geometry.coordinates
console.log(vettoreCoordinatePoligono);
var ref = snapshot.ref
var root = ref.root
var poly = new google.maps.Polyline(vettoreCoordinatePoligono)
var risultato = getSquareDivision(poly);
console.log(risultato)
return root.child('prova/' + cap + IDZona).set(risultato);

})

我在这个里面使用的函数是:

1

function getSquareDivision(polygon: google.maps.Polyline) {
var bound = getBoundsRectangle(polygon);
var lowx,
highx,
lowy,
highy,
lats = [],
lngs = [],
vertices = polygon.getPath(),
verticeslength: number = vertices.getLength()

for (var i = 0; i < verticeslength; i++) {
lngs.push(vertices.getAt(i).lng());
lats.push(vertices.getAt(i).lat());
}
lats.sort();
lngs.sort();
//VALORI VARIABILI
lowx = lats[0];
highx = lowx + 0.01;
lowy = lngs[0];
highy = lowy + 0.01;
var startHighX = highx;
var startLowX = lowx;
var sw = new google.maps.LatLng(lowx, lowy);
var ne = new google.maps.LatLng(highx, highy);
var llb = new google.maps.LatLngBounds(sw, ne);
var x = 0;
var y = 0;
var elements = [];
while (bound.contains(llb.getCenter())) {
var i = 0;
while (bound.contains(llb.getCenter())) {
sw = new google.maps.LatLng(lowx, lowy);
ne = new google.maps.LatLng(highx, highy);
llb = new google.maps.LatLngBounds(sw, ne);
var name = x + "" + y;
elements[i] = [{ name: name, geometry: llb.toJSON() }];
highx = highx + 0.01;
lowx = lowx + 0.01;
i++;
x++;
}
highx = startHighX;
lowx = startLowX;
highy = highy + 0.01;
lowy = lowy + 0.01;
sw = new google.maps.LatLng(lowx, lowy);
ne = new google.maps.LatLng(highx, highy);
llb = new google.maps.LatLngBounds(sw, ne);
y++;
x = 0;
}
return elements;
}

2

function polygonCenter(poly) {

var lowx,
highx,
lowy,
highy,
lats = [],
lngs = [],
vertices = poly.getPath();
for (var i = 0; i < vertices.length; i++) {
lngs.push(vertices.getAt(i).lng());
lats.push(vertices.getAt(i).lat());
}
lats.sort();
lngs.sort();
lowx = lats[0];
highx = lats[vertices.length - 1];
lowy = lngs[0];
highy = lngs[vertices.length - 1];
var center_x = lowx + ((highx - lowx) / 2);
var center_y = lowy + ((highy - lowy) / 2);
return (new google.maps.LatLng(center_x, center_y));
}

3

function getBoundsRectangle(poly) {

var lowx,
highx,
lowy,
highy,
lats = [],
lngs = [],
vertices = poly.getPath();
for (var i = 0; i < vertices.length; i++) {
lngs.push(vertices.getAt(i).lng());
lats.push(vertices.getAt(i).lat());
}
lats.sort();
lngs.sort();
lowx = lats[0];
highx = lats[vertices.length - 1];
lowy = lngs[0];
highy = lngs[vertices.length - 1];
var sw = new google.maps.LatLng(lowx, lowy);
var ne = new google.maps.LatLng(highx, highy);
var llb = new google.maps.LatLngBounds(sw, ne);
return llb;
}

这是我的包.json

{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"main": "lib/index.js",
"dependencies": {
"@types/google-maps": "^3.2.0",
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0",
"request":"2.88.0"
},
"devDependencies": {
"tslint": "~5.8.0",
"typescript": "~2.8.3"
},
"private": true
}

这是我的tsconfig.json

{
"compilerOptions": {
"lib": ["es6"],
"module": "commonjs",
"noImplicitReturns": true,
"outDir": "lib",
"sourceMap": true,
"target": "es6"
},
"compileOnSave": true,
"include": [
"src"
],
"files":[
"node_modules/typescript/lib/lib.es6.d.ts"
],
"exclude": [
"node_modules"
]
}

听起来您已经加载了Google Maps API的类型声明,但还没有加载实际的API实现。在任何情况下,常规的谷歌地图API仅设计用于在浏览器中运行。一个快速的网络搜索发现了这个页面,它表明在Google Cloud Functions中,您需要@google/maps包,它有自己的类型声明。

我使用另一个库解决了这个问题:http://turfjs.org/

这个库包含所有类似于googlemapsapi的实现。

我不得不修改很多代码,但现在可以了。

最新更新