Перемещение карты

Open on 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="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import type {YMapCameraRequest, YMapLocationRequest} from '@yandex/ymaps3-types';
      import {LOCATION, NEW_LOCATION_CENTER, NEW_LOCATION_BOUNDS} from './common';

      window.map = null;

      main();
      async function main() {
        // Waiting for all api elements to be loaded
        await ymaps3.ready;
        const {YMap, YMapDefaultSchemeLayer, YMapControls, YMapControlButton} = ymaps3;
        // 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({})]
        );

        // Add a shared container for YMapControlButton's and add it to the map
        const controls = new YMapControls({position: 'bottom'});
        map.addChild(controls);

        // Add YMapControlButton's that will move the map when clicked
        const changeCenterBtn = new YMapControlButton({
          text: 'Change Center',
          color: '#fff',
          background: '#007afce6',
          onClick: changeCenterBtnHandler
        });
        controls.addChild(changeCenterBtn);

        const changeBoundsBtn = new YMapControlButton({
          text: 'Change Bounds',
          color: '#fff',
          background: '#007afce6',
          onClick: changeBoundsBtnHandler
        });
        controls.addChild(changeBoundsBtn);

        const goBackBtn = new YMapControlButton({
          text: 'Go Back',
          color: '#fff',
          background: '#fd6466e6',
          onClick: goBackBtnHandler
        });

        // Moving the center of the map to a point with new coordinates
        function changeCenterBtnHandler() {
          toggleButtonVisibility(false);
          changeMapPosition(NEW_LOCATION_CENTER, {tilt: (45 * Math.PI) / 180});
        }

        // Moving the center of the map using borders with new coordinates
        function changeBoundsBtnHandler() {
          toggleButtonVisibility(false);
          changeMapPosition(NEW_LOCATION_BOUNDS, {tilt: (45 * Math.PI) / 180});
        }

        // Moving the center of the map to a point with the original coordinates
        function goBackBtnHandler() {
          toggleButtonVisibility(true);
          changeMapPosition(LOCATION, {tilt: 0});
        }

        function changeMapPosition(location: YMapLocationRequest, camera: YMapCameraRequest) {
          map.update({location: {...location, duration: 5000}, camera});
        }

        function toggleButtonVisibility(isStartPosition: boolean) {
          if (isStartPosition) {
            controls.addChild(changeCenterBtn);
            controls.addChild(changeBoundsBtn);
            controls.removeChild(goBackBtn);
          } else {
            controls.removeChild(changeCenterBtn);
            controls.removeChild(changeBoundsBtn);
            controls.addChild(goBackBtn);
          }
        }
      }
    </script>

    <!-- prettier-ignore -->
    <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>
  </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="react, typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
      import {LOCATION, NEW_LOCATION_CENTER, NEW_LOCATION_BOUNDS} from './common';
      import type {YMapCameraRequest, YMapLocationRequest} from '@yandex/ymaps3-types';

      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, YMapControls, YMapControlButton} = reactify.module(ymaps3);
        const {useState, useCallback} = React;

        function App() {
          const [location, setLocation] = useState(LOCATION);
          const [camera, setCamera] = useState < YMapCameraRequest > {tilt: 0};
          const [isStartPosition, setIsStartPosition] = useState(true);

          // Moving the center of the map to a point with new coordinates
          const changeCenterBtnHandler = useCallback(() => {
            setIsStartPosition(false);
            changeMapPosition(NEW_LOCATION_CENTER, {tilt: (45 * Math.PI) / 180});
          }, []);

          // Moving the center of the map using borders with new coordinates
          const changeBoundsBtnHandler = useCallback(() => {
            setIsStartPosition(false);
            changeMapPosition(NEW_LOCATION_BOUNDS, {tilt: (45 * Math.PI) / 180});
          }, []);

          // Moving the center of the map to a point with the original coordinates
          const goBackBtnHandler = useCallback(() => {
            setIsStartPosition(true);
            changeMapPosition(LOCATION, {tilt: 0});
          }, []);

          const changeMapPosition = useCallback((location: YMapLocationRequest, camera: YMapCameraRequest) => {
            setCamera(camera);
            setLocation({...location, duration: 5000});
          }, []);

          return (
            // Initialize the map and pass initialization parameters
            <YMap location={location} showScaleInCopyrights={true} camera={camera} ref={(x) => (map = x)}>
              {/* Add a map scheme layer */}
              <YMapDefaultSchemeLayer />

              {/* Add a shared container for YMapControlButton's */}
              <YMapControls position="bottom">
                {/* Add YMapControlButton's that will move the map when clicked */}
                {isStartPosition ? (
                  <React.Fragment>
                    <YMapControlButton
                      text="Change Center"
                      color="#fff"
                      background="#007afce6"
                      onClick={changeCenterBtnHandler}
                    />
                    <YMapControlButton
                      text="Change Bounds"
                      color="#fff"
                      background="#007afce6"
                      onClick={changeBoundsBtnHandler}
                    />
                  </React.Fragment>
                ) : (
                  <YMapControlButton text="Go Back" color="#fff" background="#fd6466e6" onClick={goBackBtnHandler} />
                )}
              </YMapControls>
            </YMap>
          );
        }

        ReactDOM.render(
          <React.StrictMode>
            <App />
          </React.StrictMode>,
          document.getElementById('app')
        );
      }
    </script>

    <!-- prettier-ignore -->
    <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>
  </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>

    <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="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {LOCATION, NEW_LOCATION_CENTER, NEW_LOCATION_BOUNDS} from './common';
      import type {YMapCameraRequest, YMapLocationRequest} from '@yandex/ymaps3-types';

      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, YMapControls, YMapControlButton} = vuefy.module(ymaps3);

        const app = Vue.createApp({
          components: {YMap, YMapDefaultSchemeLayer, YMapControls, YMapControlButton},
          setup() {
            const location = Vue.ref(LOCATION);
            const camera = Vue.ref < YMapCameraRequest > {tilt: 0};
            const isStartPosition = Vue.ref(true);

            // Moving the center of the map to a point with new coordinates
            const changeCenterBtnHandler = () => {
              isStartPosition.value = false;
              changeMapPosition(NEW_LOCATION_CENTER, {tilt: (45 * Math.PI) / 180});
            };

            // Moving the center of the map using borders with new coordinates
            const changeBoundsBtnHandler = () => {
              isStartPosition.value = false;
              changeMapPosition(NEW_LOCATION_BOUNDS, {tilt: (45 * Math.PI) / 180});
            };

            // Moving the center of the map to a point with the original coordinates
            const goBackBtnHandler = () => {
              isStartPosition.value = true;
              changeMapPosition(LOCATION, {tilt: 0});
            };

            const changeMapPosition = (newLocation: YMapLocationRequest, newCamera: YMapCameraRequest) => {
              camera.value = newCamera;
              location.value = {...newLocation, duration: 5000};
            };

            const refMap = (ref) => {
              window.map = ref?.entity;
            };
            return {
              location,
              refMap,
              camera,
              isStartPosition,
              changeCenterBtnHandler,
              changeBoundsBtnHandler,
              goBackBtnHandler
            };
          },
          template: `
            <YMap :location="location" :camera="camera" :showScaleInCopyrights="true" :ref="refMap">
                <!--Add a map scheme layer-->
                <YMapDefaultSchemeLayer />
                <!--Add a shared container for YMapControlButton's-->
                <YMapControls position="bottom">
                    <template v-if="isStartPosition">
                        <YMapControlButton
                            text="Change Center"
                            color="#fff"
                            background="#007afce6"
                            @click="changeCenterBtnHandler"
                        />
                        <YMapControlButton
                            text="Change Bounds"
                            color="#fff"
                            background="#007afce6"
                            @click="changeBoundsBtnHandler"
                        />
                    </template>
                    <YMapControlButton v-else text="Go Back" color="#fff" background="#fd6466e6" @click="goBackBtnHandler" />
                </YMapControls>
            </YMap>`
        });
        app.mount('#app');
      }
      main();
    </script>

    <!-- prettier-ignore -->
    <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>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
import type {YMapLocationRequest} from '@yandex/ymaps3-types';

export const LOCATION: YMapLocationRequest = {
  center: [37.623082, 55.75254], // starting position [lng, lat]
  zoom: 5 // starting zoom
};

export const NEW_LOCATION_CENTER: YMapLocationRequest = {
  center: [2.294587, 48.859958], // [lng, lat]
  zoom: 16.6
};

export const NEW_LOCATION_BOUNDS: YMapLocationRequest = {
  bounds: [
    [-74.045667, 40.690044], // bounds - the boundaries of the visible area of the map
    [-74.043567, 40.688628] // [[lng, lat], [lng, lat]].
  ],
  zoom: 16.6
};