openlayers - OpenLayers 读/写 GeoJSON
问题描述
我尝试将 OpenLayers 地图图层的特征存储到数据库中,因此我尝试将特征对象写入 GeoJSON 对象。但是Unsupported GeoJSON type: undefined
当我尝试读取对象时出现错误。这是我尝试过的:
const testFeature = new Feature({
geometry: new Point([0, 0]),
name: 'Test Point '
});
const geoJsonObject = new GeoJSON();
geoJsonObject.writeFeaturesObject(testFeature);
console.log(geoJsonObject);
const importObject = new GeoJSON().readFeatures(geoJsonObject);
console.log(importObject);
“geoJsonObject”的第一个日志:
{
"dataProjection": {
"code_": "EPSG:4326",
"units_": "degrees",
"extent_": [
-180,
-90,
180,
90
],
"worldExtent_": [
-180,
-90,
180,
90
],
"axisOrientation_": "neu",
"global_": true,
"canWrapX_": true,
"defaultTileGrid_": null,
"metersPerUnit_": 111319.49079327358
},
"defaultFeatureProjection": null
}
这是 importObject 日志中的错误:
Unsupported GeoJSON type: undefined
OpenLayers 版本:6.2.1 文档:https ://openlayers.org/en/latest/apidoc/module-ol_format_GeoJSON-GeoJSON.html#writeFeature
谢谢!
解决方案
除了需要具有 writeFeaturesObject 的一系列功能之外,您对 geoJsonObject 的使用是错误的
const geoJsonObject = new GeoJSON().writeFeaturesObject([testFeature]);
console.log(geoJsonObject);
const importObject = new GeoJSON().readFeatures(geoJsonObject);
console.log(importObject);
推荐阅读
- javascript - 如何在javascript中的石头剪刀布中添加5轮?
- python - 不推荐使用的警告 - mataplotlib 库中的 where 参数
- ios - 用 JSONdata 填写 UITableViewCell
- intellij-idea - IntelliJ 无法使用 JDK 11 运行 JUnit5 测试
- javascript - 如何在按钮单击操作时重新加载同一页面,即在反应中删除?
- r - 如何使用单独的 R 中的输入创建闪亮的反应函数
- .net-core - 验证收到消息的 SHA256 签名
- java - 哪个方法将从接口或类中获得覆盖?
- sql - emp 更改商店时的前 3 名员工
- javascript - JQuery html 方法中的 HTML 代码不起作用(它显示内容但 javascript 调用不起作用)