@googlemaps/map-loader

中文文档
0.2.0发布于 2021/01/21Apache-2.0TypeScriptESM + CJS近 30 天下载 1.2万

Automatic initialization and loading of the Maps JS API base map into the DOM

原始使用文档
文档目录10 节
  1. Description
  2. NPM
  3. Example
  4. Set map loader options
  5. Load the map:
  6. Or if you like, with async/await
  7. Documentation
  8. MapLoaderOptions
  9. MapsJSAPIOptions
  10. Support

Test codecov TypeScript Apache-2.0

Map Loader for Google Maps Platform JavaScript API v3

Description

Automatically loads a Google map into your website with a few lines of JavaScript or TypeScript. You provide the ID of the div where you want the map initialized or appended to, your settings for what the map looks like, and optionally your settings for loading the Maps JS API, and this module handles the rest.

Loading of the Google Maps Platform JavaScript API is taken care of for you asynchronously courteousy of @googlemaps/js-api-loader.

NPM

Available via NPM as the package @googlemaps/map-loader

Example

Set map loader options

import { GoogleMap } from 'map-loader';

const googleMapsAPIKey = "YOUR API KEY";

/* Options for how the map should initially render. */
const mapOptions = {
  center: {
    lat: 47.649196,
    lng: -122.350384
  },
  zoom: 12
}

/* Options for loading the Maps JS API. */
const apiOptions = {
  version: 'weekly',
  libraries: ['places']
}

/*
 * Set ID of the div where the map will be loaded,
 * and whether to append to that div.
 */
const mapLoaderOptions = {
  apiKey: googleMapsAPIKey,
  divId: 'google_map',
  append: true, // Appends to divId. Set to false to init in divId.
  mapOptions: mapOptions,
  apiOptions: apiOptions
};

Load the map:

// Instantiate map loader
const mapLoader = new GoogleMap();

// Load the map
mapLoader
  .initMap(mapLoaderOptions)
  .then(googleMap => {
    // returns instance of google.maps.Map
  });

Or if you like, with async/await

// Instantiate map loader
const mapLoader = new GoogleMap();

// Load the map
const await googleMap = mapLoader.initMap(mapLoaderOptions);

Documentation

If you're using TypeScript, the map loader exports interfaces for MapLoaderOptions and MapsJSAPIOptions. Options for base map settings are provided courteousy of google.maps.MapOptions in @types/googlemaps.

MapLoaderOptions

PropertyTypeDescription
apiKeystringYour API key. For information on generating an API key, see How to generate and restrict API keys for Google Maps Platform.
divIdstringThe ID of the div where you want the map initialized or appended.
mapOptionsgoogle.maps.MapOptionsSettings for how the map should appear. For a complete list of map options, see google.maps.MapOptions.
apiOptionsMapsJSAPIOptionsSetting for loading the Maps JavaScriptAPI. For a complete list of API options, see MapsJSAPIOptions.
appendbooleanOptional. Defaults to false. Sets whether to initialize the map in or append to divId.

MapsJSAPIOptions

PropertyTypeDescription
versionstringOptional. Defaults to weekly. Version of the Maps JS API to load. For more information on supported values, see Release channels and version numbers.
librariesArrayOptional. Additional Maps JS API libraries to load. Supported values are drawing, geometry, places, visualization.
languagestringOptional. Defaults to en. The language code of the language to localize the map to. For more information on supported values, see supported languages.
regionstringOptional. Defaults to us. Sets the Unicode region subtag identifier of the region to localize the map to. For more information on supported values, see Region localization.

Support

This library is community supported. We're comfortable enough with the stability and features of the library that we want you to build real production applications on it.

If you find a bug, or have a feature suggestion, please log an issue. If you'd like to contribute, please read How to Contribute.

版本与发布记录

@googlemaps/map-loader 共发布 3 个版本,首次发布于 2020/03/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.0latest2021/01/215 年前
1.0.12020/04/156 年前
0.1.02020/03/286 年前

关于 @googlemaps/map-loader 的常见问题

@googlemaps/map-loader 是什么?

Automatic initialization and loading of the Maps JS API base map into the DOM

如何安装 @googlemaps/map-loader?

在项目目录运行 npm install @googlemaps/map-loader,或使用 pnpm add / yarn add / bun add。

@googlemaps/map-loader 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@googlemaps/map-loader 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@googlemaps/map-loader 可以商用吗?

@googlemaps/map-loader 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@googlemaps/map-loader 的下载量有多少?

根据 npm 官方下载统计,@googlemaps/map-loader 在 2026/10/17 – 2026/11/15 这 30 天内被下载 11,755 次,日均约 392 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@googlemaps/map-loader 还在维护吗?

最新版本 0.2.0 发布于 2021/01/21(5 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。