代码之家  ›  专栏  ›  技术社区  ›  Ahmed Abdelmeged

firebase云函数typescript引用错误:未定义google

  •  -1
  • Ahmed Abdelmeged  · 技术社区  · 8 年前

    ReferenceError: google is not defined at /user_code/lib/file_name 我不知道这背后的原因是什么,我将键入的包添加到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": {
        "@google/maps": "^0.4.6",
        "@types/googlemaps": "^3.30.10",
        "firebase-admin": "^5.12.1",
        "firebase-functions": "^1.0.4",
        "nodemailer": "^4.6.4",
        "twilio": "^3.16.0"
      },
      "devDependencies": {
        "tslint": "^5.10.0",
        "typescript": "^2.9.2"
      },
      "private": true
    }
    

    async function getAddressFromLatAndLang(location) {
      const maps = require('@google/maps')
      const googleMapsClient = maps.createClient({
        key: 'API_KEY',
        Promise: Promise
      });
    
      const latlng = new google.maps.LatLng(location.latitude, location.longitude)
      const result = await googleMapsClient.geocode({ latlng: latlng }).asPromise()
      console.log(result)
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   xomena    8 年前

    NodeJs client library 对于谷歌地图网络服务。

    google.maps.LatLng 对象未在nodejs库中定义,此对象在Google Maps JavaScript API v3中定义,您可以在客户端使用。根据Github文档,在nodejs客户机库中,可以使用以下对象作为latlng对

    • [纬度、经度]的两项数组;

    https://googlemaps.github.io/google-maps-services-js/docs/LatLng.html

    location latitude longitude 你可以直接在

     const result = await googleMapsClient.reverseGeocode({ latlng: location }).asPromise()
    

    reverseGeocode() geocode() 方法用于将地址字符串解析为坐标。

    推荐文章