2016-11-24 66 views
1

嘗試在特徵繪製在選項卡上後發出幾何圖形1.然後嘗試使用socket.on重新繪製特徵以顯示在選項卡2上。但是由於某些原因,該特徵不是畫。使用node.js在ol3中繪製特徵

window.onload = function init() { 
    var source = new ol.source.Vector({ wrapX: false }); 
    //create a base vector layer to draw on 
    var vector = new ol.layer.Vector({ 
     source: source, 
    }); 

    var raster = new ol.layer.Tile({ 
     source: new ol.source.OSM() 
    }); 

    //create map 
    map = new ol.Map({ 
    layers: [raster, vector], 
    target: 'map', 
    controls: ol.control.defaults({ 
     attributionOptions: /** @type {olx.control.AttributionOptions} */ ({ 
      collapsible: false 
     }) 
    }), 
    view: new ol.View({ 
     center: [0,0], 
     zoom: 10 
    }) 
    }); 

    function drawShape(value) { 

     var value = value; 
     if (value !== 'None') { 
      draw = new ol.interaction.Draw({ 
       source: source, 
       type: /** @type {ol.geom.GeometryType} */ (value) 
      }); 
      map.addInteraction(draw); 

      draw.on('drawend', function (event) { 

      // Get the array of features 
      var feature = event.feature 

      try { 
       map.removeInteraction(draw); 
       socket.emit('new polygon', feature.getGeometry().getCoordinates()); 
       socket.emit('chat message', feature.getGeometry().getCoordinates()); 
      } catch (err) { } 
      }); 
     } 
    } 

    var socket = io(); 
    socket.on('new polygon', function (msg) { 

     var thing = new ol.geom.Polygon(msg); 

     var featurething = new ol.Feature({ 
      name: "Thing", 
      geometry: thing 
     }); 

     source.addFeature(featurething); 

    }); 
} 

當腳本運行時,msg包含一個座標數組。控制檯中不顯示任何內容。

msg value

我在node.js中初學者任何人知道我在做什麼錯誤

+1

可以從您的Node.js腳本中添加一些示例輸出?你的'socket.on'中的'msg'看起來像什麼?來自openlayers的任何瀏覽器控制檯錯誤? – chrki

+0

這已更新 – Khal786

回答

1

發現錯誤。在你socket.on回調您調用

source.addFeatures(featurething); 

當它應該是

source.addFeature(featurething); // single feature, no s 

source.addFeatures([featurething]); // put it in an array