openlayers加载geojson数据 *

  1. 获取GeoJSON数据,可以通过http请求获取,也可以自己定义json数据。很多小伙伴json是经纬度的坐标,不用急,往下看
    注意格式
var geojsonObject = {
        'type': 'FeatureCollection',
        'crs': {
          'type': 'name',
          'properties': {
            'name': 'EPSG:3857'
          }
        },
        'features': [{
          'type': 'Feature',
          'geometry': {
            'type': 'Point',
            'coordinates': [0, 0]
          }
        }, {
          'type': 'Feature',
          'geometry': {
            'type': 'LineString',
            'coordinates': [[4e6, -2e6], [8e6, 2e6]]
          }
        }, {
          'type': 'Feature',
          'geometry': {
            'type': 'LineString',
            'coordinates': [[4e6, 2e6], [8e6, -2e6]]
          }
        }, {
          'type': 'Feature',
          'geometry': {
            'type': 'Polygon',
            'coordinates': [[[-5e6, -1e6], [-4e6, 1e6], [-3e6, -1e6]]]
          }
        }, {
          'type': 'Feature',
          'geometry': {
            'type': 'MultiLineString',
            'coordinates': [
              [[-1e6, -7.5e5], [-1e6, 7.5e5]],
              [[1e6, -7.5e5], [1e6, 7.5e5]],
              [[-7.5e5, -1e6], [7.5e5, -1e6]],
              [[-7.5e5, 1e6], [7.5e5, 1e6]]
            ]
          }
        }, {
          'type': 'Feature',
          'geometry': {
            'type': 'MultiPolygon',
            'coordinates': [
              [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6], [-3e6, 6e6]]],
              [[[-2e6, 6e6], [-2e6, 8e6], [0, 8e6], [0, 6e6]]],
              [[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6], [3e6, 6e6]]]
            ]
          }
        }, {
          'type': 'Feature',
          'geometry': {
            'type': 'GeometryCollection',
            'geometries': [{
              'type': 'LineString',
              'coordinates': [[-5e6, -5e6], [0, -5e6]]
            }, {
              'type': 'Point',
              'coordinates': [4e6, -5e6]
            }, {
              'type': 'Polygon',
              'coordinates': [[[1e6, -6e6], [2e6, -4e6], [3e6, -6e6]]]
            }]
          }
        }]
      };
  1. 创建矢量图层Source 
  2. var vectorSource = new ol.source.Vector({
            features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
          });
  3. 很多小伙伴的json文件是经纬度的,但是map初始化的时候默认是3857的投影,这就对不上了,加载不显示,如下的geojson文件,坐标是经纬度的4326
    {
        "type": "FeatureCollection",
        "name": "R1Z",
        "crs": {
            "type": "name",
            "properties": {
                "name": "EPSG:4326"
            }
        },
        "features": [
            {
                "type": "Feature",
                "properties": {
                    "OBJECTID": 1,
                    "EntityHand": 5295,
                    "UserID": 0,
                    "alt": 45,
                    "Shape_Leng": 0.0056963466905999996
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [
                            116.4093,
                            39.47819
                        ],
                        [
                            116.4069,
                            39.4730
                        ]
                    ]
                }
            }
            
        ]
    }

     

如果geojson不显示,那多半是投影的问题,可以自己设定下投影 转换,使用{featureProjection: 'EPSG:3857'}将json转成3857的投影进行读取

var source=new ol.source.Vector({
    features: (new ol.format.GeoJSON({featureProjection: 'EPSG:3857'})).readFeatures(geojsonObject)
});

 

3.创建图层并绑定Source,给个显眼的样式

 var vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: 'red',
                            width: 5,
                        }),
                    })
      });

4.添加到map中即可,此时的map投影是3857,如果在view设置了投影为4326可用直接显示出来经纬度的,贴出完整代码:

 $.getJSON("./R1Z.geojson",
            function (data, textStatus, jqXHR) {
                debugger
                let geojsonObject = data
                let features = (new ol.format.GeoJSON({
                featureProjection: 'EPSG:3857'
                })).readFeatures(geojsonObject)

                let p = (new ol.format.GeoJSON()).readProjection(geojsonObject)



                var vectorSource = new ol.source.Vector({
                    // projection: 'EPSG:3857',
                    features: features
                });

                //矢量图层
                lineLayer = new ol.layer.Vector({
                    zIndex: 99,

                    source: vectorSource,
                    // style: styleFunction
                    style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: 'red',
                            width: 5,
                        }),
                    })

                });
                map.addLayer(lineLayer); //添加上站点的图层

            }
        );

 

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晴天蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值