未使用Google Maps JavaScript API加载程序定义"google" [英] 'google' is not defined Using Google Maps JavaScript API Loader

查看:86
本文介绍了未使用Google Maps JavaScript API加载程序定义"google"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Google Maps JavaScript API加载程序的Vue CLI项目.我使用以下代码导入加载程序:

I have a Vue CLI project that uses the Google Maps JavaScript API Loader. I import the loader using the code below:

import { Loader } from "@googlemaps/js-api-loader";

之后,我定义了加载程序,如下所示:

After that, I defined the loader, like so:

const loader = new Loader({
  apiKey: "XXXXX",
  version: "weekly",
  libraries: ["places"]
});

现在,当我尝试使用google.maps.Map对象显示地图时,出现一条错误消息,指出未定义"google".上面的所有代码都在项目的src目录中的main.js文件中,不幸的是,下面的代码是触发错误的最后一位.

Now, when I try to display a map using the google.maps.Map object, I get an error stating that 'google' is not defined. All the code above is in the project's 'main.js' file in the 'src' directory and the code below is the last bit that, unfortunately, triggers the error.

loader.load().then(() => {
  map = new google.maps.Map(document.getElementById("map"), {
    center: { lat: -34.397, lng: 150.644 },
    zoom: 8,
  });
});

我在做什么错了?

P.S.按照Google文档中的说明,我使用npm安装了 @ googlemaps/js-api-loader .

P.S. I installed @googlemaps/js-api-loader using npm, as per instructions from the Google documentation.

推荐答案

hi @Goodman L,您必须尝试一下.只需在代码的前面添加 window ..快乐的编码

hi @Goodman L you have to try it. Just add window at the front of your code.. happy coding

window.google.maps.Map

这篇关于未使用Google Maps JavaScript API加载程序定义"google"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆