1

我正在尝试使用 Openlayers 在单个图层上绘制单独的线段。该网站可以在这里看到(点击地图然后放大)。红色框表示包含路段的四叉树。绿线显示为一条折线,我希望将其视为单独的线段。

这是我现有的功能

    function DrawSegments(response){
        var SegmentsData = eval( '(' + response.responseText + ')' );

        var line;
        var lineFeature;

        if(segmentsLayer)       
            segmentsLayer.destroy();

        segmentsLayer = new OpenLayers.Layer.Vector("Segments"); 

        var points = new Array();

        var style = { strokeColor: '#00ff00', 
                    strokeOpacity: 0.8,
                    strokeWidth: 2
        };  

        map.addLayer(segmentsLayer);                    
        map.addControl(new OpenLayers.Control.DrawFeature(segmentsLayer, OpenLayers.Handler.Path));                                     

        //Used this block as code example to possibly add lines using a WKT geometry collection
        //var feature = new OpenLayers.Feature.Vector(
            //OpenLayers.Geometry.fromWKT(
                //"POLYGON((28.828125 0.3515625, 132.1875 -13.0078125, -1.40625 -59.4140625, 28.828125 0.3515625))"
            //).transform(WGS,SMP)
        //);
        //segmentsLayer.addFeatures([feature]);

        for (var i = 0; i < SegmentsData.length; i++) {         
            points.push(new OpenLayers.Geometry.Point(SegmentsData[i][0], SegmentsData[i][1]).transform(WGS,SMP));
            points.push(new OpenLayers.Geometry.Point(SegmentsData[i][2], SegmentsData[i][3]).transform(WGS,SMP));
            line = new OpenLayers.Geometry.LineString(points);              
            lineFeature = new OpenLayers.Feature.Vector(line, null, style);                 
        }
        segmentsLayer.addFeatures([lineFeature]);           
    }   

我试过了:

寻找例子

OpenLayers.Geometry.MultiLineStringOpenLayers.Geometry.Collection

我尝试将 lineFeature var 转换为数组并单独添加每个数组元素,但没有成功。文档对我没有帮助。任何帮助将不胜感激。

4

1 回答 1

3

这很简单。用于测试打开的控制台并运行以下代码段:

features = [];
json = new OpenLayers.Format.JSON();
epsg4326 = new OpenLayers.Projection('EPSG:4326');
epsg900913 = new OpenLayers.Projection('EPSG:900913');

OpenLayers.Request.GET({
    url: "http://cs.mwsu.edu/MapMatching/ViewContainers/GetRoute.php?route=input_01.txt",
    async: false,
    success: function(r){
        coords = json.read(r.responseText);
    }
});

for (var i = 0; i < coords.length; i+=1) {
    var geometry = new OpenLayers.Geometry.Point(coords[i][0], coords[i][1]).transform(epsg4326, epsg900913);
    var feature = new OpenLayers.Feature.Vector(geometry);
    features.push(feature);
}

track_layer = new OpenLayers.Layer.PointTrack('Track', {style: {strokeColor: 'FF0000',  strokeWidth: 5}});
map.addLayer(track_layer);
track_layer.addNodes(features);
于 2012-05-26T17:10:45.340 回答