0

嗨,我是 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>
4

1 回答 1

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

这就是您必须向地图添加多边形的方式,您不应该在控件中定义它们。拖动功能已经正确,只需确保您为 site_points 找到了一些东西。

于 2012-06-20T14:24:16.577 回答