Ограничения карты по rpsn

Open in CodeSandbox

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="./common.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
            import {ANIMATION_PROPS, getBounds} from './common';
            import {LOCATION, MARKERS_COORDINATES} from '../variables';
            
            window.map = null;
            
            main();
            async function main() {
                // Waiting for all api elements to be loaded
                await ymaps3.ready;
                const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapControls, YMapControl} = ymaps3;
                const {YMapDefaultMarker} = await ymaps3.import('@yandex/ymaps3-default-ui-theme');
            
                class ZoomToButtons extends ymaps3.YMapComplexEntity<{}> {
                    private _element!: HTMLButtonElement;
                    private _detachDom!: () => void;
            
                    // Method for create a DOM control element
                    _createElement() {
                        // Create a root element
                        const button = document.createElement('button');
                        button.classList.add('button');
                        button.innerText = 'Zoom to bounds';
                        button.onclick = () => {
                            const bounds = getBounds(MARKERS_COORDINATES);
                            this.root.setLocation({...ANIMATION_PROPS, bounds});
                        };
            
                        return button;
                    }
            
                    // Method for attaching the control to the map
                    _onAttach() {
                        this._element = this._createElement();
                        this._detachDom = ymaps3.useDomContext(this, this._element, this._element);
                    }
            
                    // Method for detaching control from the map
                    _onDetach() {
                        this._detachDom();
                        this._detachDom = undefined;
                        this._element = undefined;
                    }
                }
            
                // Initialize the map
                map = new YMap(
                    // Pass the link to the HTMLElement of the container
                    document.getElementById('app'),
                    // Pass the map initialization parameters
                    {location: LOCATION, showScaleInCopyrights: true},
                    // Add a map scheme layer
                    [new YMapDefaultSchemeLayer({}), new YMapDefaultFeaturesLayer({})]
                );
            
                MARKERS_COORDINATES.forEach((coordinates) => {
                    map.addChild(
                        new YMapDefaultMarker({
                            iconName: 'fallback',
                            size: 'normal',
                            coordinates
                        })
                    );
                });
            
                const control = new YMapControl().addChild(new ZoomToButtons({}));
                map.addChild(new YMapControls({position: 'bottom'}, [control]));
            }
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="./common.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
            import type {YMapLocationRequest} from '@yandex/ymaps3-types';
            import {ANIMATION_PROPS, getBounds} from './common';
            import {LOCATION, MARKERS_COORDINATES} from '../variables';
            
            window.map = null;
            
            main();
            async function main() {
                // For each object in the JS API, there is a React counterpart
                // To use the React version of the API, include the module @yandex/ymaps3-reactify
                const [ymaps3React] = await Promise.all([ymaps3.import('@yandex/ymaps3-reactify'), ymaps3.ready]);
            
                const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
                const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapControls, YMapControl} = reactify.module(ymaps3);
                const {YMapDefaultMarker} = await reactify.module(await ymaps3.import('@yandex/ymaps3-default-ui-theme'));
            
                const {useState, useCallback} = React;
            
                function App() {
                    const [location, setLocation] = useState<YMapLocationRequest>(LOCATION);
            
                    const onClick = useCallback(() => {
                        const bounds = getBounds(MARKERS_COORDINATES);
                        setLocation({...ANIMATION_PROPS, bounds});
                    }, []);
            
                    return (
                        // Initialize the map and pass initialization parameters
                        <YMap location={location} showScaleInCopyrights={true} ref={(x) => (map = x)}>
                            {/* Add a map scheme layer */}
                            <YMapDefaultSchemeLayer />
            
                            {/* Add a layer of geo objects */}
                            <YMapDefaultFeaturesLayer />
            
                            {MARKERS_COORDINATES.map((coordinates, index) => (
                                <YMapDefaultMarker key={index} coordinates={coordinates} iconName="fallback" size="normal" />
                            ))}
            
                            <YMapControls position="bottom">
                                <YMapControl>
                                    <button className="button" type="button" onClick={onClick}>
                                        Zoom to bounds
                                    </button>
                                </YMapControl>
                            </YMapControls>
                        </YMap>
                    );
                }
            
                ReactDOM.render(
                    <React.StrictMode>
                        <App />
                    </React.StrictMode>,
                    document.getElementById('app')
                );
            }
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="./common.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets=" typescript" type="text/babel">
            import {ANIMATION_PROPS, getBounds} from './common';
            import {LOCATION, MARKERS_COORDINATES} from '../variables';
            
            window.map = null;
            
            async function main() {
                // For each object in the JS API, there is a Vue counterpart
                // To use the Vue version of the API, include the module @yandex/ymaps3-vuefy
                const [ymaps3Vue] = await Promise.all([ymaps3.import('@yandex/ymaps3-vuefy'), ymaps3.ready]);
                const vuefy = ymaps3Vue.vuefy.bindTo(Vue);
                const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapControls, YMapControl, YMapListener} =
                    vuefy.module(ymaps3);
                const {YMapDefaultMarker} = await vuefy.module(await ymaps3.import('@yandex/ymaps3-default-ui-theme'));
            
                const app = Vue.createApp({
                    components: {
                        YMap,
                        YMapDefaultSchemeLayer,
                        YMapDefaultFeaturesLayer,
                        YMapControls,
                        YMapControl,
                        YMapDefaultMarker
                    },
                    setup() {
                        const refMap = (ref) => {
                            window.map = ref?.entity;
                        };
                        const location = Vue.ref(LOCATION);
            
                        const onClick = () => {
                            const bounds = getBounds(MARKERS_COORDINATES);
                            location.value = {...ANIMATION_PROPS, bounds};
                        };
            
                        return {MARKERS_COORDINATES, refMap, location, onClick};
                    },
                    template: `
                        <YMap :location="location" :showScaleInCopyrights="true" :ref="refMap">
                            <YMapDefaultSchemeLayer />
                            
                            <YMapDefaultFeaturesLayer />
            
                            <template v-for="(coordinates, index) in MARKERS_COORDINATES" :key="index">
                              <YMapDefaultMarker 
                                iconName="fallback"
                                size="normal"
                                :coordinates="coordinates"
                              />
                            </template>
            
                            <YMapControls position="bottom">
                              <YMapControl>
                                <button class="button" type="button" @click="onClick">
                                  Zoom to bounds
                                </button>
                              </YMapControl>
                            </YMapControls>
                        </YMap>`
                });
                app.mount('#app');
            }
            main();
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
import type {LngLat, YMapLocationRequest} from '@yandex/ymaps3-types';

export const LOCATION: YMapLocationRequest = {
    center: [30.326, 59.9452], // starting position [lng, lat]
    zoom: 13.5 // starting zoom
};

export const MARKERS_COORDINATES: Array<LngLat> = [
    [30.2872, 59.9119],
    [30.2398, 59.941],
    [30.2544, 59.9718],
    [30.3525, 59.9779],
    [30.4166, 59.9644],
    [30.4789, 59.9283],
    [30.5586, 59.945],
    [30.0932, 59.996],
    [30.2495, 59.9395],
    [30.283, 59.9768],
    [30.3835, 59.988],
    [30.451, 59.975],
    [30.4474, 59.9226],
    [30.4651, 59.8975],
    [30.4071, 59.8915],
    [30.3748, 59.9321],
    [30.3129, 59.931],
    [30.3411, 59.9456],
    [30.3199, 59.951],
    [30.3527, 59.9549],
    [30.3809, 59.9515],
    [30.2714, 59.9457],
    [30.2852, 59.9563],
    [30.373, 59.9404],
    [30.3599, 59.9341]
];
import type {LngLatBounds, YMapLocationRequest} from '@yandex/ymaps3-types';

ymaps3.ready.then(() => {
    ymaps3.import.registerCdn('https://cdn.jsdelivr.net/npm/{package}', '@yandex/ymaps3-default-ui-theme@0.0');
});

export const ANIMATION_PROPS: Partial<YMapLocationRequest> = {duration: 2000, easing: 'ease-in-out'};

export function getBounds(coordinates: number[][]): LngLatBounds {
    let minLat = Infinity,
        minLng = Infinity;
    let maxLat = -Infinity,
        maxLng = -Infinity;

    for (const coords of coordinates) {
        const lat = coords[1];
        const lng = coords[0];

        if (lat < minLat) minLat = lat;
        if (lat > maxLat) maxLat = lat;
        if (lng < minLng) minLng = lng;
        if (lng > maxLng) maxLng = lng;
    }

    return [
        [minLng, minLat],
        [maxLng, maxLat]
    ] as LngLatBounds;
}
.button {
    background-color: #ffffff;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    padding: 12px 16px;
}