【问题标题】:OpenLayers 3: not all lines in LineString are renderingOpenLayers 3:并非 LineString 中的所有行都在渲染
【发布时间】:2015-11-14 06:02:42
【问题描述】:

我正在尝试使用与在地图上绘制标记集合相同的坐标来绘制 lineString。标记被绘制,然后 lineString 应该绘制连接标记,lineString 使用与标记相同的坐标。

不过我遇到了奇怪的问题,有时所有线条都画了,有时只画了一条线。但通常会缺少一两行。当我在 lineString 上运行 getCoordinates() 时,它会返回与标记位置相同的所有坐标,但有些线没有绘制。

一些代码:

// location data, contains x/y coords
var locs = JSON.parse(loc_data);  
var lineCoords = [];
var lat;
var lng;

// loop through loc data and output the markers 
for (var key in locs) {
    if (locs.hasOwnProperty(key)) {

        lat = locs[key].lat;
        lng = locs[key].lng;

        // store the coords in an array to be used for the lineString
        lineCoords.push([lat,lng]);

        // create marker and add to map
        var iconFeature = new ol.Feature({
            geometry: new ol.geom.Point([lat, lng]),
        });            

        var vectorSource = new ol.source.Vector({
            features: [iconFeature]
        });

        var vectorLayer = new ol.layer.Vector({
            source: vectorSource
        });

        map.addLayer(vectorLayer);                                               
    }                
}


// draw lineString using coordinates in lineCoords array
var line = new ol.geom.LineString(lineCoords);

var layerLines = new ol.layer.Vector({
    source: new ol.source.Vector({
        features: [new ol.Feature({
                geometry: line,
                name: 'Line'
            })]
    }),
});

map.addLayer(layerLines);

上面的代码看起来很合乎逻辑,我看不出哪里有问题,就像说的那样,有时所有的线都画了,有时只画了一条。

任何人都可以对此有所了解吗?

【问题讨论】:

  • 您真的希望每次循环迭代都有一个新的源/层吗?另外,您使用的是[lat, lon],请更改为[lon, lat]
  • 我真的不知道,我只是修改了一个简单的单个标记示例以输出多个标记。我可能只是一个标记层并将每个新标记添加到该层?也许我创建没有特征的源,创建没有源的层,然后使用循环创建特征,然后使用 source.addFeatures() 和 layer.setSource()?这不应该影响 LineString 对吧?感谢您的评论
  • 结果如何?
  • 我想我已经在这个线程中尝试了各种想法,但无济于事。它偏离了优先级,自从该线程中的最后一条评论以来,我还没有回到这个问题上。准确地重新创建场景很困难,因为我从后端传递位置数据,在 plunker 中使用硬编码的位置数据不会给我带来任何问题,我想当我解决它时,我会在我的应用程序前端尝试硬编码的位置数据,看看会发生什么.到目前为止,它仍然很奇怪,它应该以没有不同的方式处理从后面传递的数据。

标签: javascript openlayers-3 multilinestring


【解决方案1】:

尝试更改:

// location data, contains x/y coords
var 
    locs = JSON.parse(loc_data),
    lineCoords = [], features = [],
    lat, lng, iconFeature
;

// loop through loc data and output the markers 
for (var key in locs) {
    if (locs.hasOwnProperty(key)) {

        lat = locs[key].lat; //is this already EPSG:3857 ?
        lng = locs[key].lng;

        // store the coords in an array to be used for the lineString
        lineCoords.push([lng, lat]);

        // create marker and add to map
        iconFeature = new ol.Feature({
            geometry: new ol.geom.Point([lng, lat]),
        });
        features.push(iconFeature);
    }                
}

var
    vectorSource = new ol.source.Vector({
        features: features //your LineString could also be included here
    }),
    vectorLayer = new ol.layer.Vector({
        source: vectorSource
    }),
    // draw lineString using coordinates in lineCoords array
    line = new ol.geom.LineString(lineCoords),
    layerLines = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: [new ol.Feature({
                geometry: line,
                name: 'Line'
            })]
        })
    })
;
map.addLayer(vectorLayer);
map.addLayer(layerLines);

【讨论】:

  • 感谢这些更改有效,但我仍然遇到相同的 lineString 问题。你能告诉我使用这种方法有什么好处吗?我认为它的内存效率更高一些?
  • 请把它放在一个 jsfiddle 上。很难知道什么是 lineString 问题。
  • 这很奇怪,我在项目之外的法线贴图上测试时使用了这种方法,效果很好。它工作得很好。我只是使用与上面相同的代码为 lineString 放置随机坐标。它工作正常,所有行都出现了。当我尝试在我正在处理的项目中使用代码时,它就开始崩溃了。我试着尽快把它弄到小提琴上
  • 这里是一个 plunker 链接Plunker,这里一切正常,与我的项目代码没有太大区别。我的项目页面有问题,不知道会不会导致这个错误,但是和bootstrap和jqueryError: Bootstrap's JavaScript requires jQuery version 1.9.1 or higher有关,会不会有影响?
  • 你在这里使用的坐标locs = JSON.parse(loc_data),是EPSG:3857吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多