2016-11-24 48 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