Changing the marker icon on mouse hover

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://js.api.mappable.world/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 {MARKER_PROPS_ACTIVE, MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, LOCATION} from '../variables';

      window.map = null;

      main();

      async function main() {
        // Waiting for all api elements to be loaded
        await mappable.ready;
        const {MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer, MMapListener} = mappable;
        const {MMapDefaultMarker} = await mappable.import('@mappable-world/mappable-default-ui-theme');

        // Initialize the map
        map = new MMap(
          // 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 MMapDefaultSchemeLayer({}),
            // Add a layer of geo objects to display the markers
            new MMapDefaultFeaturesLayer({})
          ]
        );
        const marker = new MMapDefaultMarker(MARKER_PROPS_DEFAULT);

        const listener = new MMapListener({
          layer: `${mappable.MMapDefaultFeaturesLayer.defaultProps.source}:markers`,
          // Add a listener to the map and pass the handlers functions for the events you want to process
          onMouseEnter: () => {
            marker.update(MARKER_PROPS_HOVER);
          },
          onMouseLeave: () => {
            marker.update(MARKER_PROPS_DEFAULT);
          },
          onClick: () => {
            marker.update(MARKER_PROPS_ACTIVE);
          }
        });

        const listenerMap = new MMapListener({
          layer: 'any',
          onClick: (object) => {
            if (object?.type !== 'marker') marker.update(MARKER_PROPS_DEFAULT);
          }
        });

        map.addChild(listener);
        map.addChild(listenerMap);
        map.addChild(marker);
      }
    </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>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.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://js.api.mappable.world/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 {MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, MARKER_PROPS_ACTIVE, LOCATION} from '../variables';
      import {DomEventHandler} from '@mappable-world/mappable-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 @mappable-world/mappable-reactify
        const [mappableReact] = await Promise.all([
          mappable.import('@mappable-world/mappable-reactify'),
          mappable.ready
        ]);
        const reactify = mappableReact.reactify.bindTo(React, ReactDOM);
        const {MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer, MMapListener} = reactify.module(mappable);
        const {MMapDefaultMarker} = await reactify.module(
          await mappable.import('@mappable-world/mappable-default-ui-theme')
        );

        const {useCallback, useMemo} = React;

        function App() {
          const [pointOnMarker, setPointOnMarker] = React.useState(false);
          const [markerActive, setMarkerActive] = React.useState(false);

          const onMouseEnter: DomEventHandler = useCallback(() => {
            setPointOnMarker(true);
          }, []);

          const onMouseLeave: DomEventHandler = useCallback(() => {
            setPointOnMarker(false);
          }, []);

          const onMouseClick: DomEventHandler = useCallback((object) => {
            if (object && object.type === 'marker') {
              setMarkerActive(true);
            } else {
              setMarkerActive(false);
            }
          }, []);

          const markerProps = useMemo(
            () => (markerActive ? MARKER_PROPS_ACTIVE : pointOnMarker ? MARKER_PROPS_HOVER : MARKER_PROPS_DEFAULT),
            [markerActive, pointOnMarker]
          );

          return (
            // Initialize the map and pass initialization parameters
            <MMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
              {/* Add a map scheme layer */}
              <MMapDefaultSchemeLayer />
              {/* Add a layer of geo objects to display the markers */}
              <MMapDefaultFeaturesLayer />

              <MMapDefaultMarker {...markerProps} />

              <MMapListener
                onMouseEnter={onMouseEnter}
                onMouseLeave={onMouseLeave}
                onClick={onMouseClick}
                layer={`${mappable.MMapDefaultFeaturesLayer.defaultProps.source}:markers`}
              />

              <MMapListener onClick={onMouseClick} layer="any" />
            </MMap>
          );
        }

        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>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.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://js.api.mappable.world/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 {MARKER_PROPS_ACTIVE, MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, LOCATION} from '../variables';
      import {DomEventHandler} from '@mappable-world/mappable-types';

      window.map = null;

      async function main() {
        const [mappableVue] = await Promise.all([mappable.import('@mappable-world/mappable-vuefy'), mappable.ready]);
        const vuefy = mappableVue.vuefy.bindTo(Vue);
        const {MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer, MMapListener} = vuefy.module(mappable);
        const {MMapDefaultMarker} = await vuefy.module(
          await mappable.import('@mappable-world/mappable-default-ui-theme')
        );

        const App = Vue.createApp({
          components: {
            MMap,
            MMapDefaultSchemeLayer,
            MMapDefaultFeaturesLayer,
            MMapListener,
            MMapDefaultMarker
          },
          setup() {
            const map = Vue.ref(null);
            const pointOnMarker = Vue.ref(false);
            const markerActive = Vue.ref(false);
            const refMap = (ref) => {
              window.map = ref?.entity;
            };

            const layerName = `${mappable.MMapDefaultFeaturesLayer.defaultProps.source}:markers`;

            const onMouseEnter: DomEventHandler = () => {
              pointOnMarker.value = true;
            };

            const onMouseLeave: DomEventHandler = () => {
              pointOnMarker.value = false;
            };

            const onMouseClick: DomEventHandler = (object) => {
              markerActive.value = object && object.type === 'marker';
            };

            return {
              map,
              LOCATION,
              refMap,
              pointOnMarker,
              markerActive,
              onMouseEnter,
              onMouseLeave,
              onMouseClick,
              layerName
            };
          },
          computed: {
            markerProps() {
              return this.markerActive
                ? MARKER_PROPS_ACTIVE
                : this.pointOnMarker
                ? MARKER_PROPS_HOVER
                : MARKER_PROPS_DEFAULT;
            }
          },
          template: `
      <MMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap">
        <MMapDefaultSchemeLayer/>

        <MMapDefaultFeaturesLayer/>

        <MMapDefaultMarker
          v-bind="markerProps"
        />

        <MMapListener
          :onMouseEnter="onMouseEnter"
          :onMouseLeave="onMouseLeave"
          :onClick="onMouseClick"
          :layer="layerName"
        />
        <MMapListener
          :onClick="onMouseClick"
          layer="any"
        />
      </MMap>
    `
        });

        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>
    <link rel="stylesheet" href="./common.css" />
    <link rel="stylesheet" href="../variables.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
.marker {
  cursor: pointer;

  width: 25px;
  height: 25px;
  border-radius: 50%;

  position: relative;
  transform: translate(-50%, -50%);
}
mappable.ready.then(() =>
  mappable.import.registerCdn(
    'https://cdn.jsdelivr.net/npm/{package}',
    '@mappable-world/mappable-default-ui-theme@0.0'
  )
);
import type {LngLat, MMapLocationRequest} from '@mappable-world/mappable-types';
import type {ThemesColor} from '@mappable-world/mappable-default-ui-theme';

type MarkerProps = {
  iconName: string;
  color: ThemesColor | string;
  size: string;
  coordinates: LngLat;
};

export const MARKER_PROPS_DEFAULT: MarkerProps = {
  iconName: 'airport',
  size: 'small',
  color: {day: '#000', night: '#000'},
  coordinates: [55.5358, 25.3176]
};

export const MARKER_PROPS_HOVER: MarkerProps = {
  iconName: 'airport',
  color: 'ceil',
  size: 'small',
  coordinates: [55.5358, 25.3176]
};

export const MARKER_PROPS_ACTIVE: MarkerProps = {
  iconName: 'airport',
  size: 'normal',
  color: {day: '#000', night: '#000'},
  coordinates: [55.5358, 25.3176]
};

export const LOCATION: MMapLocationRequest = {
  center: [55.5358, 25.3176], // starting position [lng, lat]
  zoom: 11.7 // starting zoom
};