@2gis/mapgl 中文文档教程

发布于 3年前 浏览 28 更新于 3年前

@2gis/mapgl

2gis MapGL API 的异步加载器,带有类型。

仅限浏览器使用!

Installation

$ npm install @2gis/mapgl

Usage

import { load } from '@2gis/mapgl';
// or const { load } = require('@2gis/mapgl');

async function start() {
    const mapglAPI = await load();

    // container — id of the div element in your html
    const map = new mapglAPI.Map('container', {
        center: [55.31878, 25.23584],
        zoom: 13,
        key: 'Your API access key',
    });

    const marker = new mapglAPI.Marker(map, {
        coordinates: [55.31878, 25.23584],
    });
}

start();

没有 await/async:

load().then((mapglAPI) => {
    const map = new mapglAPI.Map('container', {
        center: [55.31878, 25.23584],
        zoom: 13,
        key: 'Your API access key',
    });
});

默认情况下,最新 版本的 MapGL API 将被加载(https://mapgl .2gis.com/api/js)。 但是您可以指定要加载的特定版本的 MapGL API:

load('https://mapgl.2gis.com/api/js/v1.4.2').then((mapglAPI) => {});

查看我们的 MapGL API 发行说明。 需要注意的是,mapgl 加载程序版本不等于 MapGL API 版本,因为它有自己的特性。

mapgl namespace

@2gis/mapgl 导出 load 函数和 global.d.ts 文件,您可以参考这些文件来访问 mapgl 类型。

要使用 mapgl 命名空间中的类型,您需要:

  1. Add reference to node_modules/@2gis/mapgl/global.d.ts in your project.d.ts
  2. Or reference in a module, where load function is imported
/// <reference path="node_modules/@2gis/mapgl/global.d.ts" />

您可以像这样使用 mapgl 命名空间中的 MapGL 类型:

let marker: mapgl.Marker

查看示例以获取更多信息。

@2gis/mapgl

Async loader for 2gis MapGL API with typings.

Browser usage only!

Installation

$ npm install @2gis/mapgl

Usage

import { load } from '@2gis/mapgl';
// or const { load } = require('@2gis/mapgl');

async function start() {
    const mapglAPI = await load();

    // container — id of the div element in your html
    const map = new mapglAPI.Map('container', {
        center: [55.31878, 25.23584],
        zoom: 13,
        key: 'Your API access key',
    });

    const marker = new mapglAPI.Marker(map, {
        coordinates: [55.31878, 25.23584],
    });
}

start();

Without await/async:

load().then((mapglAPI) => {
    const map = new mapglAPI.Map('container', {
        center: [55.31878, 25.23584],
        zoom: 13,
        key: 'Your API access key',
    });
});

By default, latest version of MapGL API will be loaded (https://mapgl.2gis.com/api/js). But you can specify certain version of MapGL API to load:

load('https://mapgl.2gis.com/api/js/v1.4.2').then((mapglAPI) => {});

Checkout our release notes for MapGL API. Important to note, mapgl loader version is not equal to MapGL API version, cause it has its own features.

mapgl namespace

@2gis/mapgl exports load function and global.d.ts file which you can reference to have access to mapgl typings.

To use types from mapgl namespace you need:

  1. Add reference to node_modules/@2gis/mapgl/global.d.ts in your project.d.ts
  2. Or reference in a module, where load function is imported
/// <reference path="node_modules/@2gis/mapgl/global.d.ts" />

You can use typings for MapGL from mapgl namespace like this:

let marker: mapgl.Marker

Checkout example for more information.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文