Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

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 want to create a vector feature, programmatically, on my map. It's supposed to be in the A4-paperformat, for showing a sort of print preview.

I've created a control for handling this, but now I'm at the point where I need to create the feature. And I have no idea of how I can do it. Any ideas?

share|improve this question
up vote 29 down vote accepted

Creating and adding a vector feature to a layer:

var points = [
    new OpenLayers.Geometry.Point(0, 0),
    new OpenLayers.Geometry.Point(0, 100),
    new OpenLayers.Geometry.Point(100, 100),
    new OpenLayers.Geometry.Point(100, 0)
var ring = new OpenLayers.Geometry.LinearRing(points);
var polygon = new OpenLayers.Geometry.Polygon([ring]);

// create some attributes for the feature
var attributes = {name: "my name", bar: "foo"};

var feature = new OpenLayers.Feature.Vector(polygon, attributes);
var layer = new OpenLayers.Layer.Vector("Test");

However you may prefer to look at the GeoExt print controls as they support page sizes such as A4 and add preview areas to the map. You can also look at the sourcecode to see how they do this.

share|improve this answer
+1 for GeoExt print controls. They seem to work really well. You can try them – underdark May 25 '11 at 14:03
how can i also add feature name? (feature->properties->name) i need feture to be shown at the feature grid like [this], so they need a name( – k102 Mar 7 '12 at 9:36
@k102 see updated answer – geographika Mar 7 '12 at 9:53
thanks! works perfect – k102 Mar 7 '12 at 10:11

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.