【问题标题】:How can I display this geojson data on the map如何在地图上显示此 geojson 数据
【发布时间】:2021-06-24 09:13:21
【问题描述】:

我尝试测试并显示我的 geojson 数据。 但是失败了。

我的 geojson 数据:

var data= { "type": "MultiPolygon", 
"coordinates": [ [ [ 
[2875370.689553291071206, 4920425.686579301021993 ],
 [ 2875368.243538830429316, 4920426.618963065557182 ],
 [ 2875365.849673743359745, 4920427.678083536215127 ],
 [ 2875370.689553291071206, 4920425.686579301021993 ]
 ]]] }  ```


How can I display this geojson data on the map? 
regards

【问题讨论】:

    标签: openlayers geojson


    【解决方案1】:

    您有一个 GeoJSON 几何对象,因此您必须使用 OpenLayers readGeometry() 然后在矢量图层中创建一个要素来显示它。此代码假定几何坐标为 EPSG:3857 格式

    <!doctype html>
    <html lang="en">
      <head>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/css/ol.css" type="text/css">
        <style>
          html, body, .map {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
          }
        </style>
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script>
      </head>
      <body>
        <div id="map" class="map"></div>
        <script type="text/javascript">
    
          var map = new ol.Map({
            target: 'map',
            layers: [
              new ol.layer.Tile({
                source: new ol.source.OSM()
              })
            ],
            view: new ol.View()
          });
    
          var data = { "type": "MultiPolygon", 
          "coordinates": [ [ [ 
          [2875370.689553291071206, 4920425.686579301021993 ],
           [ 2875368.243538830429316, 4920426.618963065557182 ],
           [ 2875365.849673743359745, 4920427.678083536215127 ],
           [ 2875370.689553291071206, 4920425.686579301021993 ]
           ]]] };
    
          var geometry = new ol.format.GeoJSON().readGeometry(data);
    
          map.addLayer(
            new ol.layer.Vector({
              source: new ol.source.Vector({
                features: [new ol.Feature(geometry)]
              })
            })
          );
    
          map.getView().fit(geometry);
    
        </script>
      </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多