0

I'm using JavaScript to pull data from a database and use a while loop to display the contents of the database on a Bing Map.

Each entry is the database has a category and is assigned to a layer which allows the user to toggle that layer on and off.

For example, the code used to assign the location to a layer is:

businessLayer.push(pin)

Where "businessLayer" is the name of an already defined variable. I need to change "businessLayer" to "communityLayer", etc depending on the information from the database. The value of the category in the database is simply "business", "community", etc.

I've been trying to use "window[category]" (where "category" is the value from the database"). This displays only the first location on the map and gives a JavaScript error "window[...] is null or not an object".

It would appear that "window[]" does not work correctly in a while loop.

Does anyone have any idea what I'm doing wrong? It's been driving me mad for days.

Below is the code I've been using. Thanks in advance.

cb.Open(strConn);
var rb = new ActiveXObject("ADODB.Recordset"); 

function AddCameras() {

    var SQL = "select * from Table1";

    rb.Open(SQL, cb); 
    rb.MoveFirst;
    while(rb.EOF == false) {

        var icon = {icon: 'images/' + rb("Category").Value + '.png', width: 32, height: 37};
        var cat1 = rb("Category").Value.toLowerCase();
        var cat2 = cat1 + 'Layer';


        var pin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(rb("Latitude").Value, rb("Longitude").Value), icon);
        pin.Title = rb("Title").Value;
        pin.Description = rb("Details").Value;
        Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox); 
        window[cat2].push(pin);
        //businessLayer.push(pin);


        rb.MoveNext;
        }

rb.Close(); 
cb.Close();

}
rbrundritt
  • 16,570
  • 2
  • 21
  • 46
  • Why are you using "window"? I would suggest for you to create a separate EntityCollection for each category and then add the pins to it... Also, are you really querying the DB directly from Javascript? – psousa Jan 30 '15 at 22:44
  • I am creating an EntityCollection for each category and adding the pins to it. The problem is adding the pin to the EntityCollection based upon a DB entry. businessLayer.push(pin) adds the pin to the EntityCollection "businessLayer", however, this needs to change for each pin, depending on its category. I'm pulling the category from the database - "business" - and then need to use this value to create the code to add the pin to the EntityCollection to become businessLayer.push(pin). I am querying the DB directly, however, this application is only going to be available on a secure work intranet – user2804169 Feb 02 '15 at 09:51

0 Answers0