1

I made a tree view kendo to enable the user to turn off or turn on the two layers "osmlayer" and "bing_road". but the treeview is just shown and does not work. first of all chrome debugger showed that "length" property is not defined, so I changed map.layers.length to map.length. now it says that getLayersByName is not a function. how can I make this work?

here is the code:

  Create_Legend();

var selected_layers = [];
var selected_layers_name = [];
function CheckedNodeIds(nodes) {
    for (var i = 0; i < nodes.length; i++) {
        layer_id = nodes[i].id;
        if (nodes[i].hasChildren) {
            CheckedNodeIds(nodes[i].children.view());
        }
        else {
            if (nodes[i].checked) {
                map.getLayersByName(layer_id)[0].setVisibility(true);
            }
        }
    }
}
function onLegendCheck() {
    var treeView = $("#panelLayers").data("kendoTreeView");
    for (i = 0; i < map.length; i++) {
        map[i].setVisibility(false);
    }
    selected_layers = [];
    selected_layers_name = [];
    CheckedNodeIds(treeView.dataSource.view());
}

function Create_Legend() {
    $("#panelLayers").css("background-image", "linear-gradient(rgba(43, 42, 42, 0.5),rgba(97, 176, 190, 0.5)");
    //$("#panelLayers").addClass("card");
    $("#panelLayers").kendoTreeView({
        checkboxes: {
            checkChildren: true
        },
        check: onLegendCheck,
        dataSource: [
            {
                id: 1, text: "نقشه های پایه", expanded: true, items: [
                    { 'id': 'osmlayer', 'text': 'OpenStreetMap', "checked": true },
                    { 'id': 'bing_road', 'text': 'Bing Map' },
                ]
            }
        ]
    });

    onLegendCheck();
  • 1
    What is your map variable? In one place map.getLayersByName() you are using it as an OpenLayers 2 map object and elsewhere for (i = 0; i < map.length; i++) you are using it as an array. They cannot both be correct. – Mike Aug 21 '19 at 10:07
  • this is the map variable: map = new OpenLayers.Map("map", { allOverlays: true}); how can i get the code suitable for asp.net MVC? all examples in openlayers.org are suitable for npm. – MerajS Aug 21 '19 at 10:16
  • is the method getLayersByname available in OpenLayers 5? if no what is the solution? – MerajS Aug 22 '19 at 16:18
  • 1
    The equivalent code would be map.getLayers().forEach(function(layer){ if (layer.get('name') == layer_id) layer.setVisible(true); }); – Mike Aug 22 '19 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.