Yandex Maps JS API 3.0 example @yandex/ymaps3-entity-tile-loader
npm install @yandex/ymaps3-entity-tile-loader---
Yandex JS API package loading data by tiles.
Allows you to load and display on the map only those objects that are included in the tile areas
displayed on the map.
The data is loaded tile-by-tile, so you don't have to load all the data at once.


The package is located in the dist folder:
- dist/types TypeScript types
- dist/esm es6 modules for direct connection in your project
- dist/index.js Yandex JS Module
to use Yandex JS Module you need to add your module loading handler to JS API
Recommended use @yandex/ymaps3-entity-tile-loader as usual npm package.
Install @yandex/ymaps3-entity-tile-loader package from npm:
``sh`
npm install @yandex/ymaps3-entity-tile-loader
The JS API is loaded dynamically, so the package must be used after the main JS API has loaded:
`js
await ymaps3.ready; // waiting for the main JS API to load.
// ...
const {YMapEntityTileLoader} = await import('@yandex/ymaps3-entity-tile-loader');
`
You can use CDN with module loading handler in JS API on your page.
By default ymaps3.import can load self modules.ymaps3.registerCdn
Just use and ymaps3.import:
`jsymaps3.import
// register in which CDN to take the package from
ymaps3.import.registerCdn('https://cdn.jsdelivr.net/npm/{package}', '@yandex/ymaps3-entity-tile-loader@latest');
// ...
// import package from CDN
const {YMapEntityTileLoader} = await ymaps3.import('@yandex/ymaps3-entity-tile-loader');
`
`js
const {YMapFeature, YMapDefaultFeaturesLayer} = ymaps3;
const {YMapEntityTileLoader} = await ymaps3.import('@yandex/ymaps3-entity-tile-loader');
map.addChild(new YMapDefaultFeaturesLayer());
map.addChild(
new YMapEntityTileLoader({
/**
* By default, when changing tiles, old points are immediately deleted.
* But the same points may appear in the new tile, then there was no point in deleting them.
* Set the delay for applying deletion operations
*/
removalDelay: 500,
tileSize: 256, // World is 256x256 pixels on 0 zoom in Yandex
getFeatureId: (feature) => feature.id,
fetchTile: ({tx, ty, tz, sginal}) => {
return fetch(https://geodata.example/${tx}/${ty}/${tz}, {signal}).then((r) => r.json());
},
entity: (feature) =>
new YMapFeature({id: feature.id.toString(), geometry: feature.geometry, properties: feature.properties}),
onFeatureAdd: (feature) => {
console.log(feature);
},
onFeatureRemove: (feature) => {
console.log(feature);
}
})
);
`
Constructor parameters YMapEntityTileLoader:
`ts
import type {GenericFeature, LngLat, YMapEntity} from '@yandex/ymaps3-types';
export type GeojsonFeature = GenericFeature
export interface YMapEntityTileLoaderProps {
/* Tile size in pixels. World is 256x256 pixels on 0 zoom in Yandex /
readonly tileSize: number;
/**
* Function for loading data by tile, should return an array of GeoJSON features
*/
fetchTile: (args: {tx: number; ty: number; tz: number; signal: AbortSignal}) => Promise
/**
* Function for getting the id of the feature.
*/
getFeatureId: (feature: GeojsonFeature) => string;
/**
* Function for creating an YMapEntity from a feature.
*/
entity: (feature: GeojsonFeature) => YMapEntity
/**
* Function is called when a feature is added to the map.
* If the function returns false, the feature will not be added to the map.
* In this case, you should add the feature to the map yourself.
*/
onFeatureAdd: (feature: GeojsonFeature) => void | false;
/**
* Function is called when a feature is removed from the map.
* If the function returns false, the feature will not be removed from the map.
* In this case, you should remove the feature from the map yourself.
*/
onFeatureRemove: (feature: GeojsonFeature) => void | false;
/**
* By default, when changing tiles, old features are immediately deleted.
* But the same points may appear in the new tile, then there was no point in deleting them.
* Set the delay for applying deletion operations.
* @default 0
*/
removalDelay?: number;
}
`
And a React version:
`jsx
const BOUNDS = [
[53.20890963521473, 25.52765018907181],
[57.444403818421854, 24.71096299361919]
];
const LOCATION = {bounds: BOUNDS};
const [ymaps3React] = await Promise.all([ymaps3.import('@yandex/ymaps3-reactify'), ymaps3.ready]);
const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapControlButton, YMapControls, YMapFeature} =
reactify.module(ymaps3);
const {useState, useCallback} = React;
const {YMapZoomControl} = reactify.module(await ymaps3.import('@yandex/ymaps3-controls@0.0.1'));
const {YMapEntityTileLoader} = reactify.module(await ymaps3.import('@yandex/ymaps3-entity-tile-loader'));
function App() {
return (
} />
tileSize={TILE_SIZE}
getFeatureId={useCallback((feature) => feature.id, [])}
fetchTile={fetchTile}
entity={useCallback(
(feature) => (
),
[]
)}
onFeatureAdd={useCallback((entity) => {
setTotal((total) => total + entity.properties.area_sqkm);
}, [])}
onFeatureRemove={useCallback((entity) => {
setTotal((total) => total - entity.properties.area_sqkm);
}, [])}
/>
);
}
`
For react version, you can use renderDelay parameter, because react will call render` function for each feature.
To avoid unnecessary calls, you can set the delay for applying the changes.