Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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()
    })
  });
  map.addLayer(path);
}
share|improve this question
add comment

1 Answer

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();

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

track_layer = new OpenLayers.Layer.PointTrack('Track');
map.addLayer(track_layer);
track_layer.addNodes(features);
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
add comment

Your Answer

 
discard

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.