Wijmo UI for the Web
Virtual Layer
Wijmo User Guide > Widgets > Maps > Features > Virtual Layer

The virtual layer in wijmaps allows a user to display elements over the map supporting virtualization and asynchronous data loading. It can be used to display an unlimited number of elements, as long as not many of them are visible at the same time.

The division of map space is defined using the Slice option. Each map slice defines a minimum zoom level for its division, and the maximum zoom level for a slice is the minimum zoom layer of the next slice (or, if it is the last slice, its maximum zoom level is the maximum zoom of the map). In turn, each slice is divided in a grid of latitude/longitude divisions.

For example, the script below describes how to use the virtual layer to show the factories/offices/stores. At first, all factories and offices are shown on the map. When you zoom in to 10, then all the stores will be shown on the map.

Script
Copy Code
<script id="scriptInit" type="text/javascript">
    $(document).ready(function () {
        $("#maps").wijmaps({
            center: { x: 121.4, y: 31.2 },
            zoom: 3,
            targetZoom: 3
        });
                
        showVirtualLayer();
    });
    var factoriesDataBase,
        minStoreZoom = 10,
        storeSlices = Math.floor(Math.pow(2, minStoreZoom));

    function showVirtualLayer() {
        $.ajax({
            url: "../Resources/factories.json",
            success: function (result) {
                factoriesDataBase = result;
                    
                $("#maps").wijmaps("option", {
                    layers: [
                        {
                            type: "virtual",
                            slices: [
                                {                                     latitudeSlices: 1,                                                                         longitudeSlices: 1, zoom: 0 }
                            ],
                            request: requestFactories
                        },
                        {
                            type: "virtual",
                            slices: [
                                {                                     latitudeSlices: 1,                                                                         longitudeSlices: 1, zoom: 0 }
                            ],

                            request: requestOffices
                        }
                        ,
                        {
                            type: "virtual",
                            slices: [
                                {                                     latitudeSlices: 1,                                                                         longitudeSlices: 1, zoom: 0 },
                                {                                     latitudeSlices: storeSlices, longitudeSlices: storeSlices, zoom: minStoreZoom }
                            ],
                            request: requestStores
                        }]
                }).off(".layer");
            },
            dataType: "json"
        });
    }

    // virtual layer for factories
    function requestFactories(paper, zoom, maxZoom, lowerLeft, upperRight) {
        var result = { items: [] },
            factories = factoriesDataBase.factories,
            len = factories.length;
        for (var i = 0; i < len; i++) {
            var factory = factories[i];
            if (factory.longitude <= lowerLeft.x ||
                factory.longitude > upperRight.x ||
                factory.latitude <= lowerLeft.y ||
                factory.latitude > upperRight.y) {
                continue;
            }

            var item = {
                location: { x: factory.longitude, y: factory.latitude },
                size: { width: 60, height: 60 }, pinpoint: { x: 30, y: 30 }
            };

            paper.setStart();
            var img = paper.image("../images/livewidgets/factory.png", 0, 0, 60, 60);
            $(img.node).wijtooltip({ content: factory.name, position: { my: "leftcenter", at: "right+30px top+30px" } });
                
            var set = paper.setFinish();
            set.data("name", factory.name);
            item.elements = set;

            result.items.push(item);
        }

        return result;

    }

    // virtual layer for offices
    function requestOffices(paper, zoom, maxZoom, lowerLeft, upperRight) {
        var result = { items: [] },
            offices = factoriesDataBase.offices,
            len = offices.length;
            
        for (var i = 0; i < len; i++) {
            var office = offices[i];

            var item = {
                location: { x: office.longitude, y: office.latitude },
                size: { width: 60, height: 60 }, pinpoint: { x: 30, y: 30 }
            };

            paper.setStart();
            var img = paper.image("../images/livewidgets/office.png", 0, 0, 60, 60);
            $(img.node).wijtooltip({ content: office.name, position: { my: "left center", at: "right+30px top+30px"} });
                
            paper.circle(50, 10, 10).attr("fill", "#222222");
            paper.text(50, 10, office.stores).attr("fill", "white");
            var set = paper.setFinish();
            item.elements = set;
                
            result.items.push(item);
        }

        return result;
    }

    // virtual layer for stores
    function requestStores(paper, minZoom, maxZoom, lowerLeft, upperRight) {
        if (minZoom < minStoreZoom)
            return null;

        var result = { items: [] },
            stores = factoriesDataBase.stores,
            len = stores.length;

        for (var i = 0; i < len; i++) {
            var store = stores[i];
            if (store.longitude <= lowerLeft.x ||
                store.longitude > upperRight.x ||
                store.latitude <= lowerLeft.y ||
                store.latitude > upperRight.y) {
                continue;
            }

            var item = {
                location: { x: store.longitude, y: store.latitude },
                size: { width: 60, height: 60 }, pinpoint: { x: 30, y: 30 }
            };

            paper.setStart();
            var img = paper.image("../images/livewidgets/store.png", 0, 0, 60, 60);
            $(img.node).wijtooltip({ content: store.name, position: { my: "left center", at: "right+30px top+30px" } });

            var set = paper.setFinish();
            item.elements = set;

            result.items.push(item);
        }

        return result;
    }
 </script>
See Also

Reference