openlayers - Issue with for loop and if statements in javascript -
i using javascript , openlayers library in order style vector feature on map. have written following script:
var gids = response[object.keys(response)[object.keys(response).length - 1]] // data json obj // add styling - different colors complete var stylecontext = { getcolor: function (feature) { var objectkeys = object.keys(gids); // use objectkeys loop on object properties //use length (var = 0; < objectkeys.length; i++){ //alert(i); ////////////////// if(gids[i][1]=="mt"){ //alert(gids[i][1]); return "green"; } else if(gids[i][1]=="iru"){ alert(gids[i][1]); return "#780000"; //no images on line } /////////////////////// } } };
if run script without if conditions (between slashes) correct incremental value of based on maximum length of gids. when include if statements reason variable doesn't increment. remains 0.
edited
the getcolor function executed later this
// define style var defaultstyle = new openlayers.style({ fillcolor: "${getcolor}", fillopacity:"1", strokecolor: "${getcolor}", strokeopacity: "1", strokewidth: 8, cursor: "pointer", pointradius: 8 }, { context: stylecontext });
what doing wrong here? lot. d.
capture color in variable, example: color
, , return @ end of function:
getcolor: function (feature) { var color = ''; var objectkeys = object.keys(gids); // use objectkeys loop on object properties //use length (var = 0; < objectkeys.length; i++){ if(gids[i][1]=="mt"){ color = "green"; } else if(gids[i][1]=="iru"){ color = "#780000"; //no images on line } } return color; }
by looping through every property of object, , returning, you're getting last possible matching "mt" or "iru", if any. if return out of function find match, getting first possible matching "mt" or "iru".
for example, given set: [[435,'iru'],[34324,'mt'],[343,'mt']]
method return green
, , method return #780000
.
Comments
Post a Comment