Skip to content

5 - Integrating with 3d_viewer module

This example shows how to put together map_viewer module with 3d_viewer module.

You might need to check 3d_viewer documentation before.

Map Viewer

3D Viewer

Info

Click on a seat to see a panoramic view.

Code

var viewer, viewer3d;

// ---- LOADING MODULE ----
var viewer_promise = DVM.loadModule("map_viewer", {
    container: "viewer-container", // Container where the viewer will be appended
});

var viewer3d_promise = DVM.loadModule("3d_viewer", {
    venue_id: "eu-es-00008",
    container: "view3d-container"
});

Promise.all([viewer_promise, viewer3d_promise])
    .then(function(result) {
        viewer = result[0];
        viewer3d = result[1];
        start();
    })
    .catch(function(err) {
        console.error(err);
    });



function start() {
    // ---- SUBSCRIPTIONS ----
    // "first_seen" will be called the first time a section is painted on the map at level 1 (close enough to show seats)
    viewer.subscribe("first_seen", onFirstSeen);

    // Called when user clicks the map
    viewer.subscribe("click", onClick);

    // ---- LOADING ----
    viewer.loadMap({
        venue_id: "eu-es-00008-default" // Venue to be loaded. 3ddigitalvenue will provide these IDs
    })
        .then(function (obj) {
            // Successfully loaded

            // We disable automatic selection because we are going to manage it with some viewer callbacks
            viewer.flags.automatic_selection = false;
            // Node selection is limited to 1
            viewer.max_selection = 1;

            // Get sections availability
            var available_sections = getSectionAvailability();
            // Apply sections availability
            viewer.setAvailability("section", available_sections);

            console.log("LOADED!");
        })
        .catch(function (err) {
            // Error while loading
            console.error(err);
        });


    // ---- AVAILABILITY FUNCTIONS ----
    // Get sections availability. For the purpose, we generate a RANDOM availability.
    function getSectionAvailability() {
        var sections = viewer.getNodesByType("section");
        var available_sections = [];

        for (var i = 0; i < sections.length; ++i) {
            var section = sections[i];
            if (Math.random() < 0.7) {
                available_sections.push(section.id);
            }
        }

        return available_sections;
    }
}

// ---- VIEWER CALLBACKS ----
function onClick(obj) {
    var node = obj.nodes[0];
    // First, check if the click was made on a node (it can be triggered on empty space)
    if (node) {
        console.log("Click:", node.id);
        // Check if is a seat
        if ( node.type === "seat") {
            if (node.state === "available") {
                // If is available, unselect previous elements and select the new one
                viewer.unselectAll();
                viewer.select(node);
                // viewer seat and viewer3d panoramic share the same IDs, so it can be used as input to load a panoramic
                viewer3d.loadView3d({venue_id: "eu-es-00008", view_id: node.id });
            } else if (node.state === "selected") {
                // If is already selected, unselect it
                viewer.unselect(node);
            }
        }
    }
}

// Called when the viewer emits "first_seen"
function onFirstSeen(obj) {
    // Get the node or nodes affected
    var nodes = obj.nodes;

    for (var i = 0; i < nodes.length; ++i) {
        var node = nodes[i];

        console.log("first seen:", node.id);

        // Check if the node is available and is a section
        if (node && node.state === "available" && node.type === "section") {
            // Get all seats with this section as a parent
            var seats = viewer.getNodesByParent(node);
            var available = [];
            // SET RANDOM SEAT AVAILABILITY
            for (var j = 0; j < seats.length; ++j) {
                var seat = seats[j];
                if (Math.random() < 0.6) {
                    available.push(seat.id);
                }
            }

            delayAvailability(available, node.id);
        }
    }

    // Add a random timeout to see the effect on the map. This also simulates getting availability asynchronously on demand.
    function delayAvailability(availability, node_id) {
        setTimeout(function () {
            viewer.setAvailability("seat", availability, node_id);
        }, rand(250, 1000));
    }
}

// ---- HELPERS ----
function rand(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}