External modules | RasterJS API | 2GIS Documentation
RasterJS API

External modules

Besides the possibility to connect one of the packages of 2GIS modules, you can load other developers' modules from third-party servers. The maps API is compatible with most of library modules Leaflet. You can also develop and connect your own module.

To include external modules, use the DG.plugin function. Below we take a look at several examples of its use.

The usage of the DG.plugin function in the case where the module should be loaded before the map initialization:

// loading of maps API code
DG.then(function () {
    // module code loading
    return DG.plugin('https://raw.github.com/mlevans/leaflet-hash/master/leaflet-hash.js');
}).then(function () {
    // map initialization
    var map = DG.map('map', {
        center: [54.98, 82.89],
        zoom: 13,
    });
    // module initialization
    L.hash(map);
});

If the module is not needed at the initial stage of work with the map, then you can use its deferred loading and initialization (for example, when you click on the button):

// loading of maps API code
DG.then(function () {
    // map initialization
    map = DG.map('map', {
        center: [54.98, 82.89],
        zoom: 13,
    });
});

// the code can be called on demand
DG.then(function () {
    // module code loading
    return DG.plugin('https://raw.github.com/mlevans/leaflet-hash/master/leaflet-hash.js');
}).then(function () {
    // module initialization
    L.hash(map);
});

Is responsible for the loading of external modules. Function parameters:

Method Returns Description
DG.plugin( <String> url |  <Array> [<String> url, <String> url, ...] ) Promise Loads the modules. The direct links to js and css files must be specified as parameters.