2012-06-12 75 views
0

嗨,我是openlayers的新手,我從openlayers提供的示例中選取了以下代碼,我嘗試通過單擊創建4個多邊形,然後拖放每個到一個新的位置,這可能嗎?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

您應該創建一個隨機線性環(我不知道,如果你希望你的多邊形是隨機的或沒有)。線性環是一個多邊形需要的對象。

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。

相關問題