Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am rendering a vector linestring via GeoJSON on top of an OpenLayers map. I'd like to alternate the styling between two slightly different shades of blue, such that the colors alternate from one line segment to the next. Is there any way to do this, apart from using points instead of a linestring and constructing the line segments myself?

var path;

function drawPath(table, x, y, id) {
  var urlreq = "../grab.php?method=get_path&x=" + x + "&y=" + y + "&table=" + table +
                "&id=" + id;
  if( typeof(path) != 'undefined' ) { map.removeLayer(path); }
  path = new OpenLayers.Layer.Vector("Path", {
    projection: epsg900913,
    style: {
      strokeColor: "blue",
      strokeWidth: 3
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
      url: urlreq,
      format: new OpenLayers.Format.GeoJSON()
share|improve this question
up vote 1 down vote accepted

You can use PointTrack layer. See my live example here.

I think code below helps you to understand how to build layer over requested data:

features = [];
json = new OpenLayers.Format.GeoJSON();

    url: "../grab.php?method=get_path&x=" + x + "&y=" + y + "&table=" + table + "&id=" + id",
    async: false,
    success: function(r){
        features =;

track_layer = new OpenLayers.Layer.PointTrack('Track');
share|improve this answer
Thanks, that's a good example. When I use your approach of manually adding a set of points to a vector layer, it works to do addNodes on those features (they appear on the map). When I try to use my "path" layer with addNodes (e.g., track.addNodes(path.features) ), the pointTrack is not drawn on the map. Are those features not accessible in the same way when using the strategy/protocol AJAX approach? – vulture May 27 '12 at 21:04
I've modified my answer. – drnextgis May 28 '12 at 2:01
Ah, I see...a GET request with async off. Thanks very much! – vulture May 28 '12 at 6:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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