我需要在我的Cloud Functions中使用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);
})
我在其中使用的功能是:
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;
}
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));
}
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;
}
这是我的package.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"
]
}
答案 0 :(得分:1)
听起来您已经加载了Google Maps API的类型声明,但尚未加载实际的API实现。无论如何,常规的Google Maps API只能在浏览器中运行。快速的web search找到了我this page,它指示在Google Cloud Functions中,您要使用@google/maps
package来代替its own type declarations。
答案 1 :(得分:0)