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();
}