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 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
add comment

1 Answer 1

up vote 26 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.

share|improve this answer
    
+1 for GeoExt print controls. They seem to work really well. You can try them gis.uster.ch/webgis –  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(api.geoext.org/1.1/examples/feature-grid.html) –  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
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.