### Initializing Yandex Map and Clusterer (Plain JS)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This `main` function initializes a Yandex Map, adds necessary layers (scheme, feature data source, markers), defines functions (`marker`, `cluster`, `circle`) to create map markers and cluster markers, and finally creates and adds a `YMapClusterer` instance configured with a grid clustering method, generated features, and the custom marker/cluster functions.
```JavaScript
main();
async function main() {
await ymaps3.ready;
const {YMap, YMapDefaultSchemeLayer, YMapLayer, YMapFeatureDataSource} = ymaps3;
const {YMapClusterer, clusterByGrid} = await ymaps3.import('@yandex/ymaps3-clusterer@0.0.1');
map = new YMap(document.getElementById('app'), {location: LOCATION});
map
.addChild(new YMapDefaultSchemeLayer())
.addChild(new YMapFeatureDataSource({id: 'clusterer-source'}))
.addChild(new YMapLayer({source: 'clusterer-source', type: 'markers', zIndex: 1800}));
const contentPin = document.createElement('div');
contentPin.innerHTML = '
';
// Makes usual point Marker
const marker = (feature) =>
new ymaps3.YMapMarker(
{
coordinates: feature.geometry.coordinates,
source: 'clusterer-source'
},
contentPin.cloneNode(true)
);
// Makes Cluster Marker
const cluster = (coordinates, features) =>
new ymaps3.YMapMarker(
{
coordinates,
source: 'clusterer-source'
},
circle(features.length).cloneNode(true)
);
function circle(count) {
const circle = document.createElement('div');
circle.classList.add('circle');
circle.innerHTML = `\n
\n
${count}\n <\/div>\n `;
return circle;
}
const clusterer = new YMapClusterer({
method: clusterByGrid({gridSize: 64}),
features: getRandomPoints(100, BOUNDS),
marker,
cluster
});
map.addChild(clusterer);
}
```
--------------------------------
### Initializing Yandex Map Variables and Main Function (JavaScript)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Declares a global variable for the map instance and defines an asynchronous `main` function to initialize the map after the Yandex Maps library is ready. It also extracts necessary classes from the `ymaps3` object.
```JavaScript
window.map = null;
async function main() {
await ymaps3.ready;
const {YMap, YMapDefaultSchemeLayer, YMapMarker, YMapLayer, YMapFeatureDataSource} = ymaps3;
//...
}
```
--------------------------------
### Creating and Configuring Yandex Map Instance (JavaScript)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Creates a new `YMap` instance attached to the HTML element with ID 'app', sets its initial location, and adds essential layers and a feature data source required for displaying markers and clusters.
```JavaScript
const map = new YMap(document.getElementById('app'), {location: LOCATION});
map
.addChild(new YMapDefaultSchemeLayer())
.addChild(new YMapFeatureDataSource({id: 'my-source'}))
.addChild(new YMapLayer({source: 'my-source', type: 'markers', zIndex: 1800}));
```
--------------------------------
### Initializing Yandex Map and Clusterer (React)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This `main` function uses `ymaps3.import` to load `@yandex/ymaps3-reactify` and the clusterer package. It then uses `reactify` to convert Yandex Maps 3 modules into React components. It defines React functional components (`marker`, `cluster`) using `useCallback` and renders the map and clusterer within a React component (`App`) using the reactified components.
```JavaScript
main();
async function main() {
const [ymaps3React] = await Promise.all([ymaps3.import('@yandex/ymaps3-reactify'), ymaps3.ready]);
const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
const {YMap, YMapDefaultSchemeLayer, YMapLayer, YMapFeatureDataSource, YMapMarker} = reactify.module(ymaps3);
const {YMapClusterer, clusterByGrid} = reactify.module(
await ymaps3.import('@yandex/ymaps3-clusterer@0.0.1')
);
const {useState, useCallback, useMemo} = React;
const points = getRandomPoints(100, BOUNDS);
const gridSizedMethod = clusterByGrid({gridSize: 64});
function App() {
const marker = useCallback(
(feature) => (
<\/YMapMarker>
),
[]
);
const cluster = useCallback(
(coordinates, features) => (
{features.length}
<\/div>
<\/div>
<\/YMapMarker>
),
[]
);
return (
<>
(map = x)}>
<\/YMap>
<\/>
);
}
const reactRoot = ReactDOM.createRoot(document.getElementById('app'));
reactRoot.render(
<\/React.StrictMode>
);
}
```
--------------------------------
### Instantiating and Adding Yandex Maps Clusterer to Map (JavaScript)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Creates a new `YMapClusterer` instance, configuring it with the `clusterByGrid` method (specifying a grid size), the prepared feature data, and the previously defined `marker` and `cluster` rendering functions. Finally, it adds the clusterer object to the map instance.
```JavaScript
const clusterer = new YMapClusterer({
method: clusterByGrid({gridSize: 64}),
features: points,
marker,
cluster
});
map.addChild(clusterer);
```
--------------------------------
### Initializing Yandex Maps v3 with React Integration - JavaScript
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This snippet initializes the Yandex Maps v3 library, waits for it to be ready, and then imports and configures the @yandex/ymaps3-reactify package to make YMaps components usable within a React application. It extracts core map components like YMap, YMapDefaultSchemeLayer, and YMapMarker for later use.
```JavaScript
window.map = null;
main();
async function main() {
await ymaps3.ready;
const ymaps3React = await ymaps3.import('@yandex/ymaps3-reactify');
const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
const {
YMap,
YMapDefaultSchemeLayer,
YMapLayer,
YMapFeatureDataSource,
YMapMarker
} = reactify.module(ymaps3);
// ...
}
```
--------------------------------
### Importing Yandex Maps Clusterer Package (JavaScript)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Asynchronously imports the `@yandex/ymaps3-clusterer` package at version `0.0.1` and extracts the `YMapClusterer` class and the `clusterByGrid` method for use in clustering.
```JavaScript
const {YMapClusterer, clusterByGrid} = await ymaps3.import('@yandex/ymaps3-clusterer@0.0.1');
```
--------------------------------
### Importing Yandex Maps v3 Clusterer Package - JavaScript
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This code imports the @yandex/ymaps3-clusterer package, specifically version 0.0.1, and uses the reactify.module helper to make its components and methods, YMapClusterer and clusterByGrid, available for use within the React application.
```JavaScript
const {YMapClusterer, clusterByGrid} = reactify.module(
await ymaps3.import('@yandex/ymaps3-clusterer@0.0.1')
);
```
--------------------------------
### Rendering Yandex Map and Clusterer Component in React - JavaScript
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This snippet shows the main JSX structure within the App component. It renders the core Yandex Map components (YMap, layers, data source) and crucially includes the YMapClusterer component. The clusterer is configured by passing the previously defined marker and cluster render functions, the gridSizedMethod, and the points array as props. It also includes the final ReactDOM.render call to mount the app.
```JavaScript
function App() {
// ...
return <>
map = x}>
>;
}
// ...
ReactDOM.render(, document.getElementById("app"));
```
--------------------------------
### Defining Map Constants and Random Point Generation (JS)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This code defines `LOCATION` and `BOUNDS` constants used for map initialization and point generation. It includes `rndPoint` to generate a single random coordinate within bounds and `getRandomPoints` to generate an array of feature objects with random coordinates, suitable for use with map layers or clusterers.
```JavaScript
const LOCATION = {center: [55.205247, 25.077816], zoom: 10};
const BOUNDS = [
[54.58311, 25.9985],
[56.30248, 24.47889]
];
const rndPoint = (bounds) => [
bounds[0][0] + (bounds[1][0] - bounds[0][0]) * Math.random(),
bounds[1][1] + (bounds[0][1] - bounds[0][1]) * Math.random()
];
/**
* Generates randomly count points inside the BOUNDS area
*
* @param count
* @param bounds
* @returns {{geometry: {coordinates: *}, id, type: string, properties: {name: string}}[]}
*/
const getRandomPoints = (count, bounds) =>
Array.from({length: count}, () => ({
type: 'Feature',
id: i++,
geometry: {coordinates: rndPoint(bounds)},
properties: {name: 'beer shop'}
}));
```
--------------------------------
### Registering Yandex Maps 3 Clusterer Loader (JS)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This snippet adds a custom asynchronous loader function to the `ymaps3.import.loaders` array. It checks if the requested package is `@yandex/ymaps3-clusterer` and loads it either from a local path (`/dist/index.js`) if the URL includes 'localhost' or from unpkg.com otherwise. It then assigns the loaded module properties to the `ymaps3` object.
```JavaScript
ymaps3.import.loaders.unshift(async (pkg) => {
if (!pkg.includes('@yandex/ymaps3-clusterer')) {
return;
}
if (location.href.includes('localhost')) {
await ymaps3.import.script(`/dist/index.js`);
} else {
// You can use another CDN
await ymaps3.import.script(`https://unpkg.com/${pkg}/dist/index.js`);
}
Object.assign(ymaps3, window[`${pkg}`]);
return window[`${pkg}`];
});
```
--------------------------------
### Creating Yandex Map Marker Rendering Function (JavaScript)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Defines a function `marker` that takes a feature object and returns a `ymaps3.YMapMarker` instance. This function is used by the clusterer to render individual markers based on feature data.
```JavaScript
const marker = (feature) =>
new ymaps3.YMapMarker(
{
coordinates: feature.geometry.coordinates,
source: 'my-source'
},
contentPin.cloneNode(true)
);
```
--------------------------------
### Defining HTML Content for Yandex Map Marker (JavaScript)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Creates a simple HTML `div` element containing an `img` tag. This element will be used as the visual content for the individual markers displayed on the map.
```JavaScript
const contentPin = document.createElement('div');
contentPin.innerHTML = '
';
```
--------------------------------
### Creating Yandex Map Cluster Rendering Function (JavaScript)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Defines a function `cluster` that takes coordinates and features and returns a `ymaps3.YMapMarker` instance representing a cluster. It uses a helper function `circle` to create the HTML content for the cluster marker, displaying the count of features within the cluster.
```JavaScript
const cluster = (coordinates, features) =>
new ymaps3.YMapMarker(
{
coordinates,
source: 'my-source'
},
circle(features.length).cloneNode(true)
);
function circle(count) {
const circle = document.createElement('div');
circle.classList.add('circle');
circle.innerHTML = `
${count}
`;
return circle;
}
```
--------------------------------
### Defining Marker Render Function for Yandex Maps v3 Clusterer - JavaScript
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This snippet defines a memoized callback function (marker) using useCallback. This function is designed to render individual, non-clustered markers. It receives a feature object and returns a YMapMarker React component, positioning it using the feature's coordinates and including an
element as its content.
```JavaScript
const marker = useCallback(
(feature) => (
),
[]
);
```
--------------------------------
### Preparing Marker Data for Yandex Maps v3 Clusterer - JavaScript
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This snippet imports React hooks useCallback and useMemo (though only useMemo is used in the subsequent snippet, useCallback is imported here). It defines an array of geographical coordinates and then transforms this array into an array of GeoJSON-like Feature objects, which is the format expected by the Yandex Maps v3 clusterer.
```JavaScript
const {useCallback, useMemo} = React;
const coordinates = [
[54.64, 25.76],
[54.63, 25.7],
[54.43, 25.69],
[54.47, 25.68],
[54.53, 25.6],
[54.59, 25.71],
[54.5, 25.63],
[54.42, 25.57],
[54.12, 25.57],
[54.32, 25.57]
];
const points = coordinates.map((lnglat, i) => ({
type: 'Feature',
id: i,
geometry: {coordinates: lnglat},
properties: {name: 'Point of issue of orders'}
}));
```
--------------------------------
### Preparing Feature Data for Yandex Maps Clusterer (JavaScript)
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Defines an array of coordinate pairs and then maps these coordinates into an array of objects formatted as GeoJSON-like 'Feature' objects, which is the required input structure for the clusterer's features property.
```JavaScript
const coordinates = [
[54.64, 25.76],
[54.63, 25.7],
[54.43, 25.69],
[54.42, 25.127],
[54.12, 25.437]
];
const points = coordinates.map((lnglat, i) => ({
type: 'Feature',
id: i,
geometry: {coordinates: lnglat},
properties: {name: 'Point of issue of orders'}
}));
```
--------------------------------
### Defining Cluster Render Function for Yandex Maps v3 Clusterer - JavaScript
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
This snippet defines a memoized callback function (cluster) using useCallback. This function is responsible for rendering the visual representation of a cluster. It receives the cluster's coordinates and the array of features it contains, returning a YMapMarker component styled as a circle that displays the count of features within the cluster.
```JavaScript
const cluster = useCallback(
(coordinates, features) => (
),
[]
);
```
--------------------------------
### Configuring Grid Clustering Method for Yandex Maps v3 Clusterer - JavaScript
Source: https://www.npmjs.com/package/@yandex/ymaps3-clusterer?activeTab=readme/package/@yandex/ymaps3-clusterer
Inside a React functional component (App), this snippet uses the useMemo hook to create and memoize a clustering method based on a grid. It calls clusterByGrid from the clusterer package, providing a gridSize of 64 pixels to define the size of the grid cells used for clustering.
```JavaScript
function App() {
const gridSizedMethod = useMemo(() => clusterByGrid({gridSize: 64}), []);
// ...
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.