您好我是openlayers的新手,我从openlayers提供的示例中获取了以下代码,我试图通过单击创建4个多边形,然后将每个拖放到一个新位置,这是可能?
提前感谢您的帮助。
代码:
<script type="text/javascript">
var map, vectors, controls;
function init(){
map = new OpenLayers.Map('map');
var options = {numZoomLevels: 3};
var graphic = new OpenLayers.Layer.Image(
'some title',
'image.png',
new OpenLayers.Bounds(-180, -88.759, 180, 88.759),
new OpenLayers.Size(800, 200),
options
);
graphic.events.on({
loadstart: function() {
OpenLayers.Console.log("loadstart");
},
loadend: function() {
OpenLayers.Console.log("loadend");
}
});
var jpl_wms = new OpenLayers.Layer.WMS( "title",
"http://t1.hypercube.telascience.org/cgi-bin/landsat7",
{layers: "landsat7"}, options);
vectors = new OpenLayers.Layer.Vector("Vector Layer");
map.addLayers([graphic, vectors]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.addControl(new OpenLayers.Control.MousePosition());
controls = {
point: new OpenLayers.Control.DrawFeature(vectors,
OpenLayers.Handler.Point),
shape:
//*************This is what i'm trying to do***************************************************
for (i=0; i<5; i++)
{
new OpenLayers.Control.DrawFeature(vectors,
OpenLayers.Handler.RegularPolygon,
{handlerOptions: {radius: 5, sides: 4, snapAngle: 45 }}),
}
drag: new OpenLayers.Control.DragFeature(vectors)
};
for(var key in controls) {
map.addControl(controls[key]);
}
map.setCenter(new OpenLayers.LonLat(0, 0), 3);
document.getElementById('noneToggle').checked = true;
}
function toggleControl(element) {
for(key in controls) {
var control = controls[key];
if(element.value == key && element.checked) {
control.activate();
} else {
control.deactivate();
}
}
}
</script>
答案 0 :(得分:0)
您应该创建一个随机线性环(我不知道您是否希望您的多边形是随机的)。线性环是一个对象,其中的所有点都是多边形所需的。
var polygonFeatures = new Array();
for (i = 0; i < 4; i++) {
var site_points = new Array();
// Whatever points you want to us in site points
// A point is an OpenLayers.Geometry.Point
var linear_ring = new OpenLayers.Geometry.LinearRing(site_points);
// Create a feature for the polygon(s) to be in
polygonFeatures[i] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linear_ring]);
}
// Add the features to the vector layer
vectors.addFeatures([polygonFeatures]);
这是必须向地图添加多边形的方法,不应在控件中定义它们。 dragfeature已经是正确的,只要确保你为site_points找出一些东西。