OpenLayers3和Javascript - 不显示

时间:2016-07-31 23:27:39

标签: javascript gis openlayers-3

我遇到OpenLayers3的一些问题并尝试显示矢量图层。我正在从C#中收集SQL Server的几何图形并将它们传递给Javascript。从那里开始,这段代码应该在地图上绘制(代码改编自http://www.c-sharpcorner.com/article/getting-started-with-openlayers-3-and-spatial-data/)。

<script type="text/javascript">
        //services for our shapes
        var GeoArray = <%=JavaScript.Serialize(this.CGeo) %>;
        var wktReader = new ol.format.WKT();
        var featureCollection = [];
        //start getting, styling and drawing shapes
        for (var i = 0; i < GeoArray.length; i++)
        {
            var feature = wktReader.readFeature(GeoArray[i]);
            feature.getGeometry().transform('EPSG:4326', 'EPSG:3857');
            feature.setStyle(new ol.style.Style(
            {
                stroke: new ol.style.Stroke(
                {
                    color: 'red',
                    width: 3
                }),
                fill: new ol.style.Fill(
                {
                    color: 'rgba(255, 0, 0, 1)'
                })
            }));
            featureCollection.push(feature);
        }
        //create our vector source and layer
        var Vsource = new ol.source.Vector({features: featureCollection});
        var vectorLayer = new ol.layer.Vector({source: Vsource});
        //define our map and basemap layer
        var osmLayer = new ol.layer.Tile(
        {
            source: new ol.source.OSM()
        });
        var map = new ol.Map(
        {
            layers: [osmLayer, vectorLayer],
            target: 'map',
            view: new ol.View(
            {
                center: [0, 0],
                zoom: 2
            })
        });
    </script>

我们确实从C#中获取了形状(它们都是多边形,我已经做了一些警报(GeoArray.length)和警报(featureCollection.length)调用,这些调用都返回了返回的数量)但是他们没有被吸引。

顺便说一下,我们使用坐标系EPSG:3111但是当我使用它时(与EPSG相反:3857)它根本不起作用(无法获得属性)。但即使我指定它是3857(它不是),它仍然应该绘制,只是不正确地对齐(甚至删除了翻译线,没有任何反应)。

1 个答案:

答案 0 :(得分:0)

好的,我试图在OpenLayers3中使用旧的OpenLayers代码。对于任何有兴趣的人来说都是新的答案(目前这只是加载一个功能):

<script>
        //services for our shapes
        var GeoArray = <%=JavaScript.Serialize(this.CGeo) %>;
        var format = new ol.format.WKT();
        var featureCollection=[];
        var feature=format.readFeature(GeoArray[1],
        {
            dataProjection: 'EPSG:3111',
            featureProjection: 'EPSG:3857'
        });
        var VLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [feature]
            })
        });
        //define our map and basemap layer
        var osmLayer = new ol.layer.Tile(
        {
            source: new ol.source.OSM()
        });
        var map = new ol.Map(
        {
            layers: [osmLayer, VLayer],
            target: 'map',
            view: new ol.View(
            {
                center: [0, 0],
                zoom: 2
            })
        });
    </script>