openlayers层上有多个单独的行

时间:2012-05-26 04:30:55

标签: openlayers

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

这是我现有的功能

    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.MultiLineString,并 OpenLayers.Geometry.Collection

我尝试将lineFeature var转换为数组并分别添加每个数组元素而没有运气。文件没有帮助我。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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);