0

Here's what I want to do:

Show all features of the GeoJSON file using Leaflets. Connect to the database, in a database there is a table that has an ID column that matches the GeoJSON file feature, and there is a column containing the range (1-4).

IF
range = 1: red;
range = 2: yellow;
range = 3: green;
range = 4: blue;

Does anyone know this example or the idea how this should be done? I'm following this method Choropleth but I can only do this by adding a property and entering the value into the GeoJson file. But now I want to do it dynamically, by storing the range number values in the database.

enter image description here

2

Then in your style section, do something like this, referring to the range values field this way. (I called mine Range)

color:getColor(feature.properties.Range),

Now you have to create a function to style it using those values.

// Set function for color ramp
function getColor(range){
    return range == '1' ? 'red' :
    range == '2' ? 'yellow' :
    range == '3' ? 'green' :
    range == '4' ? 'blue' :
    'white';  //In case it's not defined.
}

Note: I have quotes around the range value, as if it's stored as a string, if you stored it as a number, remove the quotes.

  • Okee thanks for your answer – AdityaDS Jan 12 at 2:56

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.