Hint

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 src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
    <script src="./common.js"></script>

    <script>
      window.map = null;

      main();
      async function main() {
        await mappable.ready;
        const {
          MMap,
          MMapDefaultSchemeLayer,
          MMapDefaultFeaturesLayer,
          MMapControls,
          MMapCollection,
          MMapMarker,
          MMapFeature,

          MMapEntity
        } = mappable;

        const {MMapZoomControl} = await mappable.import('@mappable-world/mappable-controls@0.0.1');
        const {MMapHint, MMapHintContext} = await mappable.import('@mappable-world/mappable-hint@0.0.1');

        map = new MMap(document.getElementById('app'), {location: LOCATION});

        map.addChild(new MMapDefaultSchemeLayer());
        map.addChild((defaultFeatures = new MMapDefaultFeaturesLayer()));

        map.addChild(new MMapControls({position: 'right'}).addChild(new MMapZoomControl({})));

        const bounds = map.bounds;

        const markers = new MMapCollection({});
        POINTS.forEach(({coordinates, hint, color}) => {
          const marker = new MMapMarker({coordinates, properties: {hint}});
          marker.element.style.transform = 'translate(-15px, -33px)';
          marker.element.style.position = 'absolute';
          marker.element.innerHTML = markerSvg(color);

          markers.addChild(marker);
        });

        const lines = new MMapCollection({});
        LINES.forEach(({geometry, hint, style}) => {
          lines.addChild(new MMapFeature({geometry, style, properties: {hint}}));
        });

        const triangles = new MMapCollection({});
        TRIANGLES.forEach(({geometry, hint, style}) => {
          triangles.addChild(new MMapFeature({geometry, style, properties: {hint}}));
        });

        map.addChild(markers);
        map.addChild(lines);
        map.addChild(triangles);

        map.addChild(
          (hint = new MMapHint({
            layers: [defaultFeatures.layer],
            hint: (object) => object?.properties?.hint
          }))
        );

        hint.addChild(
          new (class MyHint extends MMapEntity {
            _onAttach() {
              this._element = document.createElement('div');
              this._element.className = 'my-hint';

              this._detachDom = mappable.useDomContext(this, this._element);
              this._watchContext(
                MMapHintContext,
                () => {
                  this._element.textContent = this._consumeContext(MMapHintContext)?.hint;
                },
                {immediate: true}
              );
            }
            _onDetach() {
              this._detachDom();
              this._detachDom = undefined;
            }
          })()
        );
      }
    </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" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
.my-hint {
  position: absolute;
  padding: 4px;
  background: white;
  border: 1px solid black;
  white-space: nowrap;
  opacity: 0.7;

  font-family: sans-serif;

  transform: translate(8px, -50%);
}
const BOUNDS = [
  [54.58311, 25.9985],
  [56.30248, 24.47889]
];
const LOCATION = {bounds: BOUNDS};

const seed = (s) => () => {
  s = Math.sin(s) * 10000;
  return s - Math.floor(s);
};

const rnd = seed(10000); // () => Math.random()
rnd.color = () => `rgb(${rnd() * 256}, ${rnd() * 256}, ${rnd() * 256})`;
rnd.point = () => [
  BOUNDS[0][0] + (BOUNDS[1][0] - BOUNDS[0][0]) * rnd(),
  BOUNDS[1][1] + (BOUNDS[0][1] - BOUNDS[1][1]) * rnd()
];
rnd.pointsNearby = (point, n) =>
  Array.from({length: n}, () => [point[0] + (rnd() - 0.5) * 0.5, point[1] + (rnd() - 0.5) * 0.5]);
rnd.stroke = () => [{color: rnd.color(), width: Math.floor(2 + rnd() * 6)}];

const POINTS = Array.from({length: 40}, (_, i) => ({
  color: rnd.color(),
  coordinates: rnd.point(),
  hint: `marker #${i}`
}));
const LINES = Array.from({length: 10}, (_, i) => ({
  geometry: {type: 'LineString', coordinates: [rnd.point(), rnd.point()]},
  style: {stroke: rnd.stroke()},
  hint: `line #${i}`
}));
const TRIANGLES = Array.from({length: 10}, (_, i) => ({
  geometry: {type: 'Polygon', coordinates: [rnd.pointsNearby(rnd.point(), 3)]},
  style: {stroke: rnd.stroke(), fill: rnd.color()},
  hint: `triangle #${i}`
}));

const markerSvg = (color) => `
<svg width="29" height="34" viewBox="0 0 58 67" xmlns="http://www.w3.org/2000/svg">
    <g filter="url(#filter)">
        <path d="M34.342 49.376c-3.076.93-4.687 2.979-4.831 6.147a.5.5 0 0 1-.5.477h-.022a.5.5 0 0 1-.5-.477c-.144-3.168-1.755-5.217-4.831-6.147C13.53 46.968 6 37.863 6 27 6 14.297 16.297 4 29 4s23 10.297 23 23c0 10.863-7.531 19.968-17.658 22.376z" fill="${color}" />
    </g>
    <path d="M29 67a4 4 0 1 1 0-8 4 4 0 0 1 0 8z" fill="#fff" />
    <path fill-rule="evenodd" clip-rule="evenodd" d="M29 65a2 2 0 1 0 0-4 2 2 0 0 0 0 4z" fill="${color}" />
    <defs>
        <filter id="filter" x="0" y="0" width="58" height="64" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
            <feFlood flood-opacity="0" result="BackgroundImageFix" />
            <feColorMatrix in="SourceAlpha" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha" />
            <feOffset dy="2" />
            <feGaussianBlur stdDeviation="3" />
            <feColorMatrix values="0 0 0 0 0.4 0 0 0 0 0.396078 0 0 0 0 0.380392 0 0 0 0.2 0" />
            <feBlend in2="BackgroundImageFix" result="effect1_dropShadow_9595_81428" />
            <feBlend in="SourceGraphic" in2="effect1_dropShadow_9595_81428" result="shape" />
        </filter>
    </defs>
</svg>
`;
<!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://unpkg.com/react@17/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
    <script src="./common.js"></script>

    <script type="text/babel">
      window.map = null;

      main();
      async function main() {
        const [mappableReact] = await Promise.all([
          mappable.import('@mappable-world/mappable-reactify'),
          mappable.ready
        ]);
        const reactify = mappableReact.reactify.bindTo(React, ReactDOM);
        const {
          MMap,
          MMapDefaultSchemeLayer,
          MMapDefaultFeaturesLayer,
          MMapControls,

          MMapCollection,
          MMapMarker,
          MMapFeature
        } = reactify.module(mappable);
        const {useState, useCallback} = React;

        const {MMapZoomControl} = reactify.module(await mappable.import('@mappable-world/mappable-controls@0.0.1'));
        const {MMapHint, MMapHintContext} = reactify.module(
          await mappable.import('@mappable-world/mappable-hint@0.0.1')
        );
        ReactDOM.render(
          <React.StrictMode>
            <App />
          </React.StrictMode>,
          document.getElementById('app')
        );

        function App() {
          const [location, setLocation] = useState(LOCATION);

          const getHint = useCallback((object) => object && object.properties && object.properties.hint, []);

          return (
            <MMap location={location} ref={(x) => (map = x)}>
              <MMapDefaultSchemeLayer />
              <MMapDefaultFeaturesLayer />
              <MMapControls position="right">
                <MMapZoomControl />
              </MMapControls>

              <MMapHint hint={getHint}>
                <MyHint />
              </MMapHint>

              <MMapCollection>
                {POINTS.map(({coordinates, hint, color}, ix) => (
                  <MyMarker key={ix} coordinates={coordinates} properties={{hint}} color={color} />
                ))}
              </MMapCollection>
              <MMapCollection>
                {LINES.map(({geometry, hint, style}, ix) => (
                  <MMapFeature key={ix} geometry={geometry} style={style} properties={{hint}} />
                ))}
              </MMapCollection>
              <MMapCollection>
                {TRIANGLES.map(({geometry, hint, style}, ix) => (
                  <MMapFeature key={ix} geometry={geometry} style={style} properties={{hint}} />
                ))}
              </MMapCollection>
            </MMap>
          );
        }
        function MyMarker({coordinates, properties, color}) {
          return (
            <MMapMarker properties={properties} coordinates={coordinates}>
              <div
                dangerouslySetInnerHTML={{__html: markerSvg(color)}}
                style={{transform: 'translate(-15px, -33px)', position: 'absolute'}}
              ></div>
            </MMapMarker>
          );
        }
        function MyHint() {
          const ctx = React.useContext(MMapHintContext);
          return <div className="my-hint">{ctx && ctx.hint}</div>;
        }
      }
    </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" />
  </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://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>
    <script src="./common.js"></script>

    <script>
      window.map = null;

      main();
      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,
          MMapControls,
          MMapCollection,
          MMapMarker,
          MMapFeature
        } = vuefy.module(mappable);
        const {MMapZoomControl} = vuefy.module(await mappable.import('@mappable-world/mappable-controls@0.0.1'));
        const {MMapHint, MMapHintContext} = vuefy.module(
          await mappable.import('@mappable-world/mappable-hint@0.0.1')
        );

        const MyMarker = Vue.defineComponent({
          name: 'MyMarker',
          props: ['coordinates', 'properties', 'color'],
          setup({coordinates, properties, color}) {
            return () =>
              Vue.h(MMapMarker, {coordinates, properties}, () =>
                Vue.h('div', {
                  innerHTML: markerSvg(color),
                  style: {transform: 'translate(-15px, -33px)', position: 'absolute'}
                })
              );
          }
        });
        const PointsCollection = Vue.defineComponent({
          name: 'PointsCollection',
          setup() {
            return () =>
              Vue.h(MMapCollection, () =>
                POINTS.map(({coordinates, hint, color}, ix) =>
                  Vue.h(MyMarker, {key: ix, coordinates, color, properties: hint})
                )
              );
          }
        });
        const LinesCollection = Vue.defineComponent({
          name: 'LinesCollection',
          setup() {
            return () =>
              Vue.h(MMapCollection, () =>
                LINES.map(({geometry, hint, style}, ix) =>
                  Vue.h(MMapFeature, {key: ix, geometry, style, properties: hint})
                )
              );
          }
        });
        const TrianglesCollection = Vue.defineComponent({
          name: 'TrianglesCollection',
          setup() {
            return () =>
              Vue.h(MMapCollection, () =>
                TRIANGLES.map(({geometry, hint, style}, ix) =>
                  Vue.h(MMapFeature, {key: ix, geometry, style, properties: hint})
                )
              );
          }
        });
        const MyHint = Vue.defineComponent({
          name: 'MyHint',
          setup() {
            const ctx = Vue.inject(MMapHintContext);
            return {ctx};
          },
          template: '<div className="my-hint">{{ctx && ctx.hint}}</div>'
        });

        const app = Vue.createApp({
          components: {
            MMap,
            MMapDefaultSchemeLayer,
            MMapDefaultFeaturesLayer,
            MMapControls,
            MMapZoomControl,
            MMapHint,
            MyHint,
            PointsCollection,
            LinesCollection,
            TrianglesCollection
          },
          setup() {
            const getHint = (object) => {
              if (object?.properties?.hint) {
                return object?.properties?.hint;
              } else if (object?.properties && typeof object?.properties === 'string') {
                return object?.properties;
              }
            };
            const refMap = (ref) => {
              window.map = ref?.entity;
            };
            return {LOCATION, getHint, refMap};
          },
          template: `
                        <MMap :location="LOCATION" :ref="refMap">
                            <MMapDefaultSchemeLayer />
                            <MMapDefaultFeaturesLayer />
                            <MMapControls position="right">
                                <MMapZoomControl></MMapZoomControl>
                            </MMapControls>
                            <MMapHint :hint="getHint">
                                <MyHint />
                            </MMapHint>
                            <PointsCollection />
                            <LinesCollection />
                            <TrianglesCollection />
                        </MMap>`
        });
        app.mount('#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" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>