none
Dynamically add entity collection RRS feed

  • Question

  • Hello all,

    I'm trying to build layers dynamicly but it doesn't work. So, does anyone could say me how to do ?

    If I build them manually, it works :

    layer_500= new Microsoft.Maps.EntityCollection();
    layer_22= new Microsoft.Maps.EntityCollection();
    layer_23= new Microsoft.Maps.EntityCollection();
    layer_28= new Microsoft.Maps.EntityCollection();
    layer_107= new Microsoft.Maps.EntityCollection();
    layer_47= new Microsoft.Maps.EntityCollection();
    layer_500= new Microsoft.Maps.EntityCollection();
    layer_88= new Microsoft.Maps.EntityCollection();
    layer_153= new Microsoft.Maps.EntityCollection();
    layer_87= new Microsoft.Maps.EntityCollection();

    However if I try to build them dynamicly they are not found when I want to use them :

    for (var i=0; i <data.length; i++)
     {
    layer=("layer_" +data[i].id);
    layer= new Microsoft.Maps.EntityCollection();
    }

    Could you explain me how I must do it ?

     

    Thanks for your help !

    • Moved by Ricky_Brundritt Friday, March 9, 2012 1:58 PM (From:Bing Maps: Map Control and Web services Development)
    Thursday, January 13, 2011 7:12 PM

Answers

  • Try this inside your for loop ( I just tested it - it works)

    eval("var layer_" + data[i].id + "= new Microsoft.Maps.EntityCollection()");

    So your for loop looks like this

    for (var i=0; i <data.length; i++)
     {
    eval("var layer_" + data[i].id + "= new Microsoft.Maps.EntityCollection()");
    }

     


    Aditya Tadakaluru MCPD, MCITP, MCTS(Bing Maps)
    • Marked as answer by FrEd1820 Thursday, January 13, 2011 9:24 PM
    Thursday, January 13, 2011 7:39 PM

All replies

  • Try this inside your for loop ( I just tested it - it works)

    eval("var layer_" + data[i].id + "= new Microsoft.Maps.EntityCollection()");

    So your for loop looks like this

    for (var i=0; i <data.length; i++)
     {
    eval("var layer_" + data[i].id + "= new Microsoft.Maps.EntityCollection()");
    }

     


    Aditya Tadakaluru MCPD, MCITP, MCTS(Bing Maps)
    • Marked as answer by FrEd1820 Thursday, January 13, 2011 9:24 PM
    Thursday, January 13, 2011 7:39 PM
  • for (var i=0; i <data.length; i++)
     {
    layer=("layer_" +data[i].id);
    layer= new Microsoft.Maps.EntityCollection();
    }

    Think about what you are doing there.   There's only one variable called 'layer'.  First, you set it to a string value.  Then you destroy the string you just created, and replace it with an EntityCollection object.  Next time around the loop, you destroy that object and replace it with a string ; then you destroy the new string in turn and replace it with an EntityCollection ...

    Have you considered keeping an array of layers?

    var MyLayers = new Array();
    for (var i=0; i <data.length; i++)
     {
    MyLayers[i] = new Microsoft.Maps.EntityCollection();
    }

    Thursday, January 13, 2011 8:15 PM
  • The objective to define a single layer name like this ( with an id from my database)


    layer=("layer_" +data[i].id);
    layer= new Microsoft.Maps.EntityCollection();  


    it's to find for example the numbers of pushpin 's layer.

    function layer_is_rempli(id_categorie){
        try{
            var monLayer=eval('layer_'+id_categorie);
            var nombre = monLayer.getLength();
        }catch(e){}
        return nombre;
    }

    This code is good and it return the numbers of pushpin in the layer.

     

    Moreover, I not find the equivalent in V7 for "layer = map.GetShapeLayerByIndex(id)"  of the v6.3

     

     

    Thursday, January 13, 2011 8:52 PM
  • Try this inside your for loop ( I just tested it - it works)

    eval("var layer_" + data[i].id + "= new Microsoft.Maps.EntityCollection()");

    So your for loop looks like this

    for (var i=0; i <data.length; i++)
     {
    eval("var layer_" + data[i].id + "= new Microsoft.Maps.EntityCollection()");
    }

     


    Aditya Tadakaluru MCPD, MCITP, MCTS(Bing Maps)


    GOOD !! Thanks you very much !!

    The complete function :

    function creerLayer(data){
               
                for (var i=0; i <data.length; i++)
                    {
                    eval("layer_" + data[i].id + "= new Microsoft.Maps.EntityCollection()");
               
                    }

    }

    Thursday, January 13, 2011 9:23 PM