Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

6.4 KB
Raw Blame
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Kepler.gl embedded map</title>

    <!--Uber Font-->
    <link
      rel="stylesheet"
      href="https://d1a3f4spazzrp4.cloudfront.net/kepler.gl/uber-fonts/4.0.0/superfine.css"
    />

    <!--MapBox css-->
    <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.css" rel="stylesheet" />
    <link href="https://unpkg.com/maplibre-gl@^3/dist/maplibre-gl.css" rel="stylesheet" />

    <!--
      React >= 19 no longer ships UMD builds, so we load React/Redux as ES modules
      via esm.sh and re-expose them as globals for the kepler.gl UMD bundle.

      es-module-shims runs in "shim mode" so that import maps and module scripts
      work when this file is opened directly from disk (file:// protocol), where
      native ES module imports are blocked by CORS.
    -->
    <script>
      window.esmsInitOptions = {shimMode: true};
    </script>
    <script
      src="https://unpkg.com/[email protected]/dist/es-module-shims.js"
      crossorigin="anonymous"
    ></script>

    <script type="importmap-shim">
      {
        "imports": {
          "react":              "https://esm.sh/[email protected]",
          "react/jsx-runtime":  "https://esm.sh/[email protected]/jsx-runtime",
          "react-dom":          "https://esm.sh/[email protected]?external=react",
          "react-dom/client":   "https://esm.sh/[email protected]/client?external=react",
          "redux":              "https://esm.sh/[email protected]",
          "react-redux":        "https://esm.sh/[email protected]?external=react,react-dom,redux",
          "styled-components":  "https://esm.sh/[email protected]?external=react,react-dom"
        }
      }
    </script>

    <!--
      Expose the ES modules as globals, then dynamically load the kepler.gl UMD
      bundle. Once it's ready, run the bootstrap scripts below.
    -->
    <script type="module-shim">
      import * as React           from 'react';
      import * as ReactDOM        from 'react-dom';
      import * as ReactDOMClient  from 'react-dom/client';
      import * as Redux           from 'redux';
      import * as ReactRedux      from 'react-redux';
      import * as StyledComponents from 'styled-components';

      window.React          = React.default || React;
      window.ReactDOM       = Object.assign({}, ReactDOM.default || ReactDOM, ReactDOMClient);
      window.Redux          = Redux.default || Redux;
      window.ReactRedux     = ReactRedux.default || ReactRedux;
      window.styled         = Object.assign(StyledComponents.default, StyledComponents);

      function loadScript(src) {
        return new Promise(function(resolve, reject) {
          var script = document.createElement('script');
          script.src = src;
          script.crossOrigin = 'anonymous';
          script.onload = resolve;
          script.onerror = reject;
          document.head.appendChild(script);
        });
      }

      loadScript('https://unpkg.com/[email protected]/umd/keplergl.min.js')
        .then(function() {
          document.querySelectorAll('script[type="text/kepler-bootstrap"]').forEach(function(node) {
            var script = document.createElement('script');
            script.text = node.textContent;
            document.body.appendChild(script);
          });
        })
        .catch(function(err) {
          console.error('kepler.gl: failed to load UMD bundle', err);
        });
    </script>

    <style type="text/css">
      body {
        margin: 0;
        padding: 0;
        overflow: hidden;
      }
    </style>

    <!--
      Provide your MapBox Token if you want to use Mapbox basemaps.
      Leave as 'pk.xxx.yyy' to use the default MapLibre (Carto) basemap — no token required.
    -->
    <script>
      window.MAPBOX_TOKEN = 'pk.xxx.yyy';
    </script>
  </head>
  <body>
    <div id="app"></div>

    <script type="text/kepler-bootstrap">
      /** STORE **/
      const reducers = (function createReducers(redux, keplerGl) {
        return redux.combineReducers({
          keplerGl: keplerGl.keplerGlReducer
        });
      })(Redux, KeplerGl);

      const middleWares = (function createMiddlewares(keplerGl) {
        return keplerGl.enhanceReduxMiddleware([]);
      })(KeplerGl);

      const enhancers = (function createEnhancers(redux, middles) {
        return redux.applyMiddleware(...middles);
      })(Redux, middleWares);

      const store = (function createStore(redux, enhancers) {
        return redux.createStore(reducers, {}, redux.compose(enhancers));
      })(Redux, enhancers);
      /** END STORE **/

      /** COMPONENTS **/
      const KeplerElement = (function makeKeplerElement(react, keplerGl, mapboxToken) {
        return function App() {
          var _useState = react.useState({width: window.innerWidth, height: window.innerHeight});
          var dimensions = _useState[0];
          var setDimensions = _useState[1];

          react.useEffect(function() {
            function onResize() {
              setDimensions({width: window.innerWidth, height: window.innerHeight});
            }
            window.addEventListener('resize', onResize);
            return function() { window.removeEventListener('resize', onResize); };
          }, []);

          return react.createElement(
            'div',
            {style: {position: 'absolute', left: 0, width: '100vw', height: '100vh'}},
            react.createElement(keplerGl.KeplerGl, {
              mapboxApiAccessToken: mapboxToken,
              id: 'map',
              width: dimensions.width,
              height: dimensions.height
            })
          );
        };
      })(React, KeplerGl, MAPBOX_TOKEN);

      const app = (function createReactReduxProvider(react, reactRedux, KeplerElement) {
        return react.createElement(
          reactRedux.Provider,
          {store},
          react.createElement(KeplerElement, null)
        );
      })(React, ReactRedux, KeplerElement);
      /** END COMPONENTS **/

      /** Render **/
      (function render(reactDOM, app) {
        const root = reactDOM.createRoot(document.getElementById('app'));
        root.render(app);
      })(ReactDOM, app);

      /**
       * Customize map.
       * Use the store to dispatch kepler.gl actions and load data.
       * Example:
       *   store.dispatch(KeplerGl.addDataToMap({ datasets: [...], config: {...} }));
       */
      (function customize(keplerGl, store) {
        // store.dispatch(keplerGl.toggleSplitMap());
      })(KeplerGl, store);
    </script>
  </body>
</html>