Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:22:02+00:00 2026-06-04T19:22:02+00:00

I am developing a OpenLayers application which does dynamic classification of a vector layer.

  • 0

I am developing a OpenLayers application which does dynamic classification of a vector layer. Therefore I am trying to loop through a specific number of thematic classes (categories) and define a filter for each class inside.

A OpenLayers object called OpenLayers.Filter.Function represents each of the filters. Each OpenLayers.Filter.Function overwrites the evaluate() function of the OpenLayers class.

My code looks like this:

var rules = new Array();

for (var i = 0; i < numClasses; i++) {
    filter_x = function() {
    return new OpenLayers.Filter.Function({
            evaluate: function(attributes) {
                // some code to define the filter
                // uses specific values: array[i] & array[i+1
    }
    var rule_x = new OpenLayers.Rule({
        filter: filter_x,
        symbolizer: { fillColor: colors[i],
                    fillOpacity: 0.5, strokeColor: "white"}
    });
    rules.push(rule_x);
}
thematicStyle.addRules(rules);

Unfortunately this does not work. It seems that all filters execute the evaluate()-function of the last loop cycle.

Do you have any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:22:03+00:00Added an answer on June 4, 2026 at 7:22 pm

    You’re doing a closure over i which is mutable, that is, for all of your rules, the value of i will be the same in the end, namely, numClasses - 1. i is the same variable, shared for all iterations. No copy is created inside the body of the loop.

    Here’s the right way to do it:

    var rules = new Array();
    
    for (var i = 0; i < numClasses; i++) (function (i) {
        filter_x = function() {
        return new OpenLayers.Filter.Function({
                evaluate: function(attributes) {
                    // some code to define the filter
                    // uses specific values: array[i] & array[i+1
        }
        var rule_x = new OpenLayers.Rule({
            filter: filter_x,
            symbolizer: { fillColor: colors[i],
                        fillOpacity: 0.5, strokeColor: "white"}
        });
        rules.push(rule_x);
    })(i);
    thematicStyle.addRules(rules);
    

    In future versions of Javascript, you’ll be able to do let j = i inside the for-loop, but for now on, you’re stuck with these semantics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an application, part of which uses OpenLayers (calling a Geoserver-served WMS) displaying
Currently developing an application using the newest version of symfony, obtained through PEAR. This
When developing an application which mostly interacts with a database, what is a good
i am developing an api which uses timemap library with openlayers.. i send my
i am developing an online mapping application using OpenLayers + OpenStreetMaps. i need help
Developing a C# .NET 2.0 WinForm Application. Need the application to close and restart
When developing a .Net web application, if a run time error is generated, it
Developing a multilingual application in VB.Net 2008, Im able to add resources to forms
Developing a desktop application based on Java + Swing I faced the problem of
developing an application in Access. The access file will be located on a db

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.