16

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?

1 Answer 1

30

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");
layer.addFeatures([feature]);

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.

3
  • +1 for GeoExt print controls. They seem to work really well. You can try them gis.uster.ch/webgis
    – underdark
    Commented May 25, 2011 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(api.geoext.org/1.1/examples/feature-grid.html)
    – k102
    Commented Mar 7, 2012 at 9:36
  • @k102 see updated answer Commented Mar 7, 2012 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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