06 – Javascript API (experimental)

Warning!!! This is an experimental feature and therefore may change a lot in the future (including breaking changes).

The Situm Map Viewer offers a Javascript API so you can interact programmatically with it. This API is based on postMessage(), which is a method in JavaScript that allows communication between two windows, such as an iframe and its parent window, or between two separate browser windows.

Using the Javascript API #

From the developer console #

If you want to do a quick test of the Javascript API, the fastest way to do it is to interact with it from the Developer Console. For example, in Chrome, you may open the Developer Tools, go to the Console and copy & paste the following snippet:

// You may subscribe to the API messages ... 
window.addEventListener("message", (e) => {
  var data = e.data;
  const parsedData = JSON.parse(data.replaceAll("'", '"'));
  // ... and operate on the received data, which contains "type" and a "payload" fields
  switch (parsedData.type) {
    //In this example, we just care for POI (de)selected events
    case "cartography.poi_selected":
    case "cartography.poi_deselected":
      console.log("Known message received. Type: ", parsedData.type, " Payload: ", parsedData.payload);
      break;
    // ... but the API issues more messages!!
    default:
      console.info("Unknown message received: ", parsedData);
      break;
  }
});

After you do this, if you click & unclick a POI, you should see something like this in your Developer Console:

From a webpage #

First of all, you’ll need to integrate the Map Viewer in your webpage. One way to do this is by using an iframe, as explaned here. Then, on your code you may subscribe to the messages sent by the MapViewer using the window.addEventListener method. The following HTML code illustrates a full example:

<!DOCTYPE html>
<html>
  <head>
    <style>
      body,
      html {
        padding: 0;
        margin: 0;
      }
    </style>
  </head>
  <body>
    <!-- Integrate the Map Viewer as an iframe in your webpage -->
    <iframe
      id="viewer"
      style="width: 100%; height: 100vh"
      src="http://map-viewer.situm.com/?email=YOUR_EMAIL@DOMAIN.COM&apikey=YOUR_APIKEY"
      frameborder="0"
    ></iframe>
    <script>
      //After the DOM loads...
      document.addEventListener("DOMContentLoaded", function (event) {
        // ... you may subscribe to the API messages ... 
        window.addEventListener("message", (e) => {
          var data = e.data;
          const parsedData = JSON.parse(data.replaceAll("'", '"'));
          // ... and operate on the received data, which contains "type" and a "payload" fields
          switch (parsedData.type) {
            //In this example, we just care for POI (de)selected events
            case "cartography.poi_selected":
            case "cartography.poi_deselected":
              console.log("Known message received. Type: ", parsedData.type, " Payload: ", parsedData.payload);
              break;
            // ... but the API issues more messages!!
            default:
              console.info("Unknown message received: ", parsedData);
              break;
          }
        });
      });
    </script>
  </body>
</html>

Message structure and supported events #

When you receive postMessage from our API, you should pay attention to the following fields:

  • type, which is always “message“. This is why you subscribe to the API using window.addEventListener(“message“, …)
  • data, which is a JSON field that you need to parse. On the inside, it contains:
    • type. Indicates the type of event issued by the Map Viewer: cartography.poi_selected, cartography.poi_deselected, etc.
    • payload. Additional data, such as the POI Identifier in case of a cartography.poi_selected event.

The following table documents the currently implemented events with their data.type and data.payload:

EventDescriptiondata.typedata.payload
POI selectedUser has selected a POIcartography.poi_selected{id: POI_ID}
where POI_ID is the POI identifier (number)
POI deselectedUser has deselected a previously selected POIcartography.poi_deselected{id: POI_ID}
where POI_ID is the POI identifier (number)
Floor selectedUser has changed the view to a certain floorcartography.floor_selected{id: FLOOR_ID}
where FLOOR_ID is the floor identifier (number)
Building selectedUser has changed the view to a certain buildingcartography.building_selected{id: BUILDING_ID}
where BUILDING_ID is the building identifier (number)

You can find a detailed and updated list of API messages in the next link: https://developers.situm.com/sdk_documentation/map-viewer/

Suscríbete a nuestro boletín

INFORMACIÓN BÁSICA SOBRE PROTECCIÓN DE DATOS

Responsable del tratamiento: SITUM TECHNOLOGIES, S.L.
Contacto: Responsable del tratamiento: situm@situm.es
Responsable de protección: dpo@situm.es
Finalidad y base legal: Gestionar el envío de newsletter de SITUM sólo con consentimiento.
Legitimación: Consentimiento expreso del interesado.
Destinatarios: Los datos no serán cedidos a terceros salvo obligación legal.
Plazo de conservación: Mientras la parte interesada permanezca suscrita al newsletter (en cada newsletter enviado por Situm estará disponible un link para darse de baja).
Derechos: El interesado podrá revocar en cualquier momento su consentimiento, así como ejercitar los derechos de oposición, acceso, conservación, rectificación, limitación, supresión de datos y no ser objeto de una decisión basada únicamente en el tratamiento automatizado de datos, dirigiéndose por escrito a SITUM en las direcciones indicadas.
Información Adicional: Puede consultar la información adicional y detallada sobre Protección de Datos en nuestra política de privacidad.

Por favor, descarga tu copia aquí.

Muchas gracias por descargar nuestro whitepaper. No dudes en contactar con nosotros si quieres saber más sobre cómo nuestras soluciones pueden ayudar a tu negocio.


Cerrar ventana