如何通过npm安装Google地图? [英] How to install google maps through npm?

查看:70
本文介绍了如何通过npm安装Google地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

npm上是否有适用于Google地图的软件包?还是我真的应该粘贴这个

Is there any package available on npm for google maps? Or am I really supposed to paste this

<script src="https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY">
</script>

到我的index.html并在每次刷新时下载此js文件?

to my index.html and download this js file on every refresh?

这太烦人了,因为有时我会收到 ReferenceError:未定义google .

This is super annoying, because sometimes I get ReferenceError: google is not defined.

推荐答案

或者我真的应该粘贴这个吗到我的index.html并在每次刷新时下载此js文件?

Or am I really supposed to paste this to my index.html and download this js file on every refresh?

.这是唯一的方法.有一些软件包可以为您动态地执行此操作,但是行为是相同的.

Yes. This is the only way to do so. There are some packages to dynamically do this for you, but the behavior is the same.

重申一下,没有用于在NPM上为网络环境加载Google Maps JavaScript的官方软件包.其他人引用的 @ google/地图仅用于节点.

To reiterate, there is no official package for loading the Google Maps JavaScript for the web environment on NPM. The @google/maps referenced by others is for node only.

未定义google 错误.

更新-2020/01/17

我写了 @ googlemaps/js-api-loader 以帮助动态加载脚本并支持诺言.

I wrote @googlemaps/js-api-loader to help load the script dynamically and support promises.

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

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

loader
  .load()
  .then(() => {
    new google.maps.Map(div, mapOptions);
  })
  .catch(e => {
    // do something
  });

这篇关于如何通过npm安装Google地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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