在OpenLayers2中以编程方式创建矢量功能


16

我想以编程方式在地图上创建矢量要素。它应该是A4纸格式的,用于显示一种打印预览。

我已经创建了用于处理此问题的控件,但是现在我需要创建该功能。而且我不知道该怎么做。有任何想法吗?

Answers:


30

创建矢量要素并将其添加到图层:

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

但是,您可能更希望查看GeoExt打印控件,因为它们支持页面尺寸(例如A4)并向地图添加预览区域。您还可以查看源代码,以了解它们如何执行此操作。


+1用于GeoExt打印控件。他们似乎工作得很好。您可以尝试使用它们gis.uster.ch/webgis
underdark

我还如何添加功能名称?(特征- >属性- >名)我需要feture以像特征网格[此]被显示,所以需要一个名称(api.geoext.org/1.1/examples/feature-grid.html
K102

@ k102请参阅更新的答案
geographika,
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.