如何在地图上显示这个geojson数据



我尝试测试并显示我的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

您有一个GeoJSON几何对象,因此必须使用OpenLayersreadGeometry(),然后在矢量层中创建一个功能来显示它。此代码假定几何坐标为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>

相关内容

  • 没有找到相关文章

最新更新