javascript - 如何将geojson点添加到地图
问题描述
我有一组点都是 Geojson 格式。我想知道如何将这些点添加到地图中。我提到了一些问题,有些使用了以下
geojson.features
但就我而言,当我打电话时,.features
我会收到undefined
. 请查看以下链接
https://gis.stackexchange.com/questions/282208/drawing-a-polygon-over-a-point-in-openlayers
请让我知道如何正确地将 geojson 点添加到地图上。
geojson点数组:
["{\"type\":\"Point\",\"coordinates\":[6.73648711211944,51.1144539430392]}", "{\"type\":\"Point\",\"coordinates\":[6.73628689838002,51.1141803601161]}", "{\"type\":\"Point\",\"coordinates\":[6.73648765865954,51.1141088970156]}", "{\"type\":\"Point\",\"coordinates\":[6.7368576747845,51.1141914104954]}", "{\"type\":\"Point\",\"coordinates\":[6.73641043458919,51.1141123530415]}", "{\"type\":\"Point\",\"coordinates\":[6.73655920234526,51.1144536412875]}", "{\"type\":\"Point\",\"coordinates\":[6.73614395402386,51.1142745754592]}", "{\"type\":\"Point\",\"coordinates\":[6.73642965248497,51.1141830888474]}", "{\"type\":\"Point\",\"coordinates\":[6.73641750225145,51.1144305273589]}", "{\"type\":\"Point\",\"coordinates\":[6.73671503777408,51.1141886499952]}", "{\"type\":\"Point\",\"coordinates\":[6.73671498356252,51.1141886122033]}", "{\"type\":\"Point\",\"coordinates\":[6.73657228945022,51.1141858496955]}", "{\"type\":\"Point\",\"coordinates\":[6.73655926013517,51.1144536295152]}", "{\"type\":\"Point\",\"coordinates\":[6.7364332915611,51.1141086186341]}", "{\"type\":\"Point\",\"coordinates\":[6.73642959244726,51.1141831229718]}", "{\"type\":\"Point\",\"coordinates\":[6.73688015139349,51.1142293782007]}
解决方案
geojsons 仅包含几何图形,因此您需要从数组中读取每个几何图形并创建一个可以显示在地图上的要素。
<!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>
<title>OpenLayers example</title>
</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({
center: [0, 0],
zoom: 2
})
});
var geojsons = [
"{\"type\":\"Point\",\"coordinates\":[6.73648711211944,51.1144539430392]}",
"{\"type\":\"Point\",\"coordinates\":[6.73628689838002,51.1141803601161]}",
"{\"type\":\"Point\",\"coordinates\":[6.73648765865954,51.1141088970156]}",
"{\"type\":\"Point\",\"coordinates\":[6.7368576747845,51.1141914104954]}",
"{\"type\":\"Point\",\"coordinates\":[6.73641043458919,51.1141123530415]}",
"{\"type\":\"Point\",\"coordinates\":[6.73655920234526,51.1144536412875]}",
"{\"type\":\"Point\",\"coordinates\":[6.73614395402386,51.1142745754592]}",
"{\"type\":\"Point\",\"coordinates\":[6.73642965248497,51.1141830888474]}",
"{\"type\":\"Point\",\"coordinates\":[6.73641750225145,51.1144305273589]}",
"{\"type\":\"Point\",\"coordinates\":[6.73671503777408,51.1141886499952]}",
"{\"type\":\"Point\",\"coordinates\":[6.73671498356252,51.1141886122033]}",
"{\"type\":\"Point\",\"coordinates\":[6.73657228945022,51.1141858496955]}",
"{\"type\":\"Point\",\"coordinates\":[6.73655926013517,51.1144536295152]}",
"{\"type\":\"Point\",\"coordinates\":[6.7364332915611,51.1141086186341]}",
"{\"type\":\"Point\",\"coordinates\":[6.73642959244726,51.1141831229718]}",
"{\"type\":\"Point\",\"coordinates\":[6.73688015139349,51.1142293782007]}"
];
var features = [];
geojsons.forEach(function(geojson) {
var geometry = new ol.format.GeoJSON().readGeometry(geojson, {
dataProjection: 'EPSG:4326',
featureProjection: map.getView().getProjection()
});
features.push(new ol.Feature(geometry));
});
var source = new ol.source.Vector({
features: features
});
map.addLayer(new ol.layer.Vector({
source: source,
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: 'red'
}),
stroke: new ol.style.Stroke({
color: 'blue',
width: 2
})
})
})
}));
map.getView().fit(source.getExtent());
map.getView().adjustZoom(-4);
</script>
</body>
</html>
推荐阅读
- git - 如何在不授予对 gitlab 中的 repo 访问权限的情况下向用户授予拉取请求权
- c# - 在 Azure 上部署的 Web 无法访问 .less 文件(错误代码 404)。本地 IIS 部署正常工作
- git - 变基后,我的功能分支上的 master 的长时间提交和文件更改
- javascript - 尽管路径明显正确,如何修复 Node.js 文件路径错误?
- javascript - 在 React 组件中设置多个 Google Map 引脚
- html - 我有一个 html 内容,它占用了页面的完整宽度,但在 PDF 中它只占用了 50% 的宽度
- android - Android将listview数据转换为pdf文件并保存
- angular - Angular File Input 所选文件未显示
- mips - MIPS 嵌套过程
- java - 交易块之外会发生什么?