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?

up vote 30 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");
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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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