Wednesday 25 October 2017

How to draw lines with different colors in Openlayers 2?



I want to display multiple polylines with different colors in openlayers. Can any one guide me regarding this.


I have done this in google maps. but i don't know how to do it in openlayers. I am newbie to Openlayers.


enter image description here



Answer



Every feature has a style property which is null by default because it inherits the parent vector layer's style. But you can set the style for each feature:



enter image description here


DEMO LINK


Code Example:


var myFirstLineStyle = OpenLayers.Util.applyDefaults(myFirstLineStyle, OpenLayers.Feature.Vector.style['default']);

myFirstLineStyle.strokeColor = "#ffffff";
myFirstLineStyle.strokeWidth = 8;
firstFeature.style = myFirstLineStyle;

var mySecondLineStyle = OpenLayers.Util.applyDefaults(mySecondLineStyle, OpenLayers.Feature.Vector.style['default']);
mySecondLineStyle.strokeColor = "#000000";
mySecondLineStyle.strokeWidth = 4;
secondFeature.style = mySecondLineStyle;

If you change the styles of the features after they've been added to the layer you must call layer.redraw().



More on Styling


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...