I am using leaflet and geojson of some regions to show the related calculations about those regions on map by color.

I calculated each region value by a function which is called calculation(). The output of this function is a simple float. Here is what I have done:

I use onEachFeature for loading each feature. Then I use Ajax to do some calculations. (I use Jquery here) I save the calculation results in an array. then, I use .done to set the style of each feature (which is a region) to set its color.

Here is my code:

var region=L.geoJSON(qom, {onEachFeature:
function (feature, layer) {
    $.ajax({    //send the ajax with following parameter
        timeout: 2000,
        url:    "../tool/calculation.php", //the php we are sending info too and has got 
        data: { },    // multiple data sent using ajax
        success: function (data) {    //now we have data
            $.each(data, function(calculation_related_key,calculation_related_content) {
                region_array_calculation.push([the_region_id, calculation_related_content['result']]);
    }).done(function (){
        L.geoJSON(qom, {style : set_feature_style(feature, region_array_calculation)}).addTo(mymap);

Here is setting style code:

function set_feature_style (feature, calculation_array) {
for (i=0;i<calculation_array.length;i++) {
    if (calculation_array[i][0]==feature.properties.region_id) {
        return {
            weight: 0.5,
            opacity: 0.3,
            color: 'Black',
            dashArray: '3',
            fillOpacity: 0.2,

Here I set the color:

function get_feature_color(input) {
var x= input;
switch (true)   {
case (x>=0 && x<=0.5):
    return 'blue';
case (x>0.5 && x<=1):
    return 'green';

But unfortunately the result I get is all in blue.

However the calculation for some of the regions is more than 0.5.

  • 2
    Shouldn't it be a switch over x and not just true? switch(x) – Alex Parij Nov 25 '16 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.