Javascript 使用node.js在ol3中绘制功能

Javascript 使用node.js在ol3中绘制功能,javascript,html,node.js,openlayers,Javascript,Html,Node.js,Openlayers,在选项卡1上绘制特征后尝试发射几何体。然后尝试使用要显示在选项卡2上的socket.on重新绘制该功能。但是,由于某些原因,未绘制特征 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:

在选项卡1上绘制特征后尝试发射几何体。然后尝试使用要显示在选项卡2上的socket.on重新绘制该功能。但是,由于某些原因,未绘制特征

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包含一个坐标数组。控制台中没有显示任何内容


我是node.js的初学者。任何人都知道我做错了什么

发现了错误。在您的
套接字中。在
回调中,您正在调用

source.addFeatures(featurething);
应该是什么时候

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


能否从node.js脚本中添加一些示例输出?
msg
在您的
socket.on
中看起来像什么?openlayers是否有浏览器控制台错误?此错误已更新
source.addFeatures([featurething]); // put it in an array